tags:

views:

57

answers:

4
    $(document).ready(function () {
 doSomething(1);    
 $('#pp').click( doSomething(2) );//why is this called?? I didn't click the button..
});

function doSomething(v) {
 alert(v);
}
</script>

<body>

<input type="button" id="pp" value="asdf" />

I need a function to be called on load and click. But somehow doSomething() is called two times on load. What's going on..??

A: 
$(document).ready(function () {
 doSomething(1);    
 $('#pp').click( function(){doSomething(2);} );
});
Francisco
A: 

You need to wrap what you pass to .click() in a function {}. Otherwise it executes when the .click() line executes and the result is passed to .click().

Amber
A: 

I think there's an error in your event-binding. It should be:

$('#pp').bind('click', function () { doSomething(2); });
Oliver
+2  A: 

Change the ready fragment to

$(document).ready(function() {
  doSomething(1);
  $('#pp').click(function() {
    doSomething(2);
  });
});
Kranu