-4

How do I send parameters in onclick function using Javascript. The following are the variables, need to set in onclick function.

var address ="Madison Avenue New York NY 10022";
var title ="Sydney NSW";

$('#MapDiv').html('<a href="javascript:void(0);" onclick="showGoogleMap();"><img src="assets/CommonAssets/time.png" /></a>');
Praveen
  • 55,303
  • 33
  • 133
  • 164
vishnu
  • 405
  • 2
  • 10
  • 17
  • possible duplicate of [Passing parameters in Javascript onClick event](http://stackoverflow.com/questions/3495679/passing-parameters-in-javascript-onclick-event) – Carth Jan 07 '14 at 05:37
  • Thans carth..Its working fine – vishnu Jan 07 '14 at 05:42

2 Answers2

1

Setup an ID for <a> and then in jQuery, use:

$('#id').click(function(address, title){
 //code here
})
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
  • This will fail because the `a` tag is created dynamically. So try to do with `on` event handler attachment. – Praveen Jan 07 '14 at 05:43
  • Good catch. :) Deleting this answer in 5 mins. – Rahul Desai Jan 07 '14 at 05:47
  • Why're you deleting the answer, instead update it. `$(document).on('click', '#id', function(address, title){ //code here })` – Praveen Jan 07 '14 at 05:47
  • oh wait, I got it working here: http://jsfiddle.net/kqMr9/ (I had a stupid typo). – Rahul Desai Jan 07 '14 at 05:48
  • Thats my bad actually I didn't noticed that you have loaded the click event after generating the `a` tag whereas check this fiddle http://jsfiddle.net/kqMr9/3/ you will understand what I tried to convey. – Praveen Jan 07 '14 at 06:02
1
var address = "Madison Avenue New York NY 10022";
var title = "Sydney NSW";
$('#MapDiv').html('<a href="javascript:void(0);" onclick="showGoogleMap(\''+address+'\',\''+title+'\');"><img src="assets/CommonAssets/time.png" /></a>');
SKG
  • 510
  • 4
  • 20