Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function called on page load not jquery .click

This function gets called on page load, not when .somebutton is clicked. I'm not sure why. I want to be able to pass the variables so that I may use this function in multiple places with different values. Thank you very much.

var i = "this be i";
var u = "this be u";

function dosomething (var1, var2){
    console.log(var1 + " and " + var2);
}

$(".somebutton").click(dosomething(i,u));
like image 833
seal6105 Avatar asked Feb 18 '23 14:02

seal6105


1 Answers

You are passing value returned by dosomething(i,u) to click handler. This is why it ise executing without clicking, it is happening as soon as you call your function (that is: dosomething(i,u)). You need to wrap your function call inside anonymous function:

$(".somebutton").click(function() { dosomething(i,u); } );
like image 76
Zbigniew Avatar answered Feb 20 '23 03:02

Zbigniew