I’m attempting to create a postcode lookup JQuery script that’ll be able to be used multiple times on a page, without having to duplicate the script for every address on the page. The Find button has the class “postcodeLookup” and the attribute “address” which is how I intended to get the script to populate the right address (it uses JQuery Populate plugin) & the inputs are named address[line1], where “address” is changeable (home[line1], office[line1], etc.).
The problem is how to get the JSON that populates the address to use the variable contents rather than the literal word “address”?
//postcode lookups
$(".postcodeLookup").click(function(){
alert("I am The Postcode Finder...\nPretending to find the address...\nFound it!");
var address = $(this).attr('address');
$("form").populate({
address: {
line1: "First Line of Addr.",
line2: "Line 2!",
line3: "Line 3 of The Address",
postcode: "PO1 1PO"
}
});
$(".addressArea").slideDown('fast');
});
Now, when
addrNameisoffice, that will be the same as writing… which, in turn, is exactly the same thing as writing
And that little piece of knowledge will be immensely useful in all aspects of javascript.
For instance
Will actually create 10 variables in the window object (i.e. public variables) called
var0,var1, …,var9.Ok, so that wasn’t immensely useful, but you get the idea.