views:

61

answers:

2

I would like to know if I can include some Jquery inside another JQuery function,

I'm changing the src attribute of the #ggMap item

onclick="$('#ggMap').attr('src', 'googleMap.php?x=690&y=390&address=');return false;"

and Thishis is what I'm trying to do:

onclick="$('#ggMap').attr('src', 'googleMap.php?x=690&y=390&address=$('#address').val();');return false;"

I want the value of #address to be included in the url.

A: 

Almost. This should work (keep an eye for what is JavaScript, and what is JavaScript string):

onclick="$('#ggMap').attr('src', 'googleMap.php?x=690&y=390&address=' + $('#address').val()); return false;"

Of cource, it is recommended to write actual function, and use jQuery to bind them to events.

Kobi
A: 

That use of an unescaped & is invalid HTML and will break if your parameter names are unlucky enough to match an entity name.

You will also need to encodeURIComponent the address value to make it a valid URL when the address contains spaces or most punctuation.

For these escaping reasons and others, I'd avoid doing so much work in an inline attribute value, and bind the click handler from JavaScript instead.

$('#whateverElement').click(function() {
    var address= $('#address').val();
    var url= 'googleMap.php?x=690&y=390&address='+encodeURIComponent(address);
    $('#ggMap').attr('src', url);
});
bobince