I'm trying to set some data on my buttons such that it can be accessed onclick
. I'm having no problem using JSON in a button's data attribute where the key value is a string. However, I can't figure out how to set the values to be a function.
What I'd like to happen upon clicking the button in this demo code is for the click event to call the function option1()
which will alert the string "hello outside".
The error I'm getting is this:
Uncaught TypeError: Property 'option1' of object #<Object> is not a function
HTML (JSFiddle is here: http://jsfiddle.net/NDaEh/32/):
<button type='button' data-button='{"option1": "option1", "option2":
"option2"}'>click1</button>
JS:
var data='hello outside';
var option1=function(data){
alert(data)
}
$('button').click(function(){
//var data='hello inside';
$(this).data('button').option1(data); // should alert 'hello outside'
});
Thoughts?