Calling A Jquery Function From Variable Value
I have to call Jquery functions using a variable value. Let consider below scenario: var func = 'append'; $(document).func('
Append Welco
Solution 1:
Functions can be accessed using []
syntax, just like regular properties of an object:
var func = "append";
$(document)[func]("<div>Append Welcome</div>");
func = "prepend";
$(document)[func]("<div> Prepend Welcome</div>");
func = "html";
$(document)[func]("<div> Insert Html Welcome</div>");
Solution 2:
You can call a function using a variable name with bracket notation: https://jsfiddle.net/leojavier/mw4hm5ma/
var app = {
alert : function(message){
alert(message)
}
}
var funcName = "alert"
app[funcName]('this is a message')
I'm creating an object with a method inside... then I can use the variable value as a name for that function ex Object[variable](arguments)
I hope this helps...
Post a Comment for "Calling A Jquery Function From Variable Value"