I have a generic javascript class that creates an html input element. The code allows me to pass a function as a string that will be called whenever the value of the element changes, using jQuery’s change method. I’m using the following code:
1. var changeFunction = "runThisCode()";
2.
3. var elem = $("<input/>").attr(id: someID, type: "text", size: 20);
4. elem.change(function () {
5. var fn = new Function(changeFunction);
6. fn();
7. });
I’d like to pass the id of the element in the change function. Is there a way for me to add a parameter at either on line 5 or 6?
You can provide arguments name in
Functionwhich will be available in created function body. jsfiddle