call function with parameter in jQuery -


when click control id of button1 calls function called getmakes, want able pass parameter it. in case id of control receiving data. doing wrong?

$(function () {          $('#button1').click(getmakes("'#output'"))         $('#buttonclear').click(function () {             $('#output').html('');         }); }); function getmakes(myvar) {    $.ajax({     type: "post",     url: "webservice.asmx/dbaccess2",     data: "{}",     contenttype: "application/json; charset=utf-8",     datatype: "json",     success: function (msg) {         var response = msg.d;         $.each(response, function (index, value) {             $(myvar).append(new option(value, index));         });     },     failure: function (msg) {         alert('failure');     } }); 

}

oj , jacob have right solution, didn't explain why. code,

$('#button1').click(getmakes("'#output'")) 

passes return value of getmakes click. you're calling function right , there (when line gets executed -- not when user clicks it). that's not want, want call function on click event argument. that, need call parameterless function in turn calls function arguments. easiest way anonymous function, others have suggested.

also note line of code has quote marks , missing semi-colon.. not sure if intentional.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -