I'm writing in jQuery and want to make something happen when an element is clicked. The function I want called requires parameters, and the event must be watched for at all times, so the handler is in $(document).ready(). Here's what I mean:
"use strict"
$(document).ready(function(){
$("<button>").each(
$(this).click(doSomething)
);
});
function doSomething(message){
alert(message);
}
The problem is that doSomething needs a message that it can alert. However, were I to change the code to this:
$(document).ready(function(){
$("<button>").each(
$(this).click(doSomething("Hello world"))
);
});
function doSomething(message){
alert(message);
}
Then "Hello world" would be alerted when the page loads, and clicking buttons would do nothing. How do I keep the behavior of the first way, but pass the method a parameter?