json - Jquery Mobile: created a button with JQuery, trying to attach click function to it -
i retrieving data remote server via json , attaching button it:
$.getjson('http://www.mysite.com/jsond1.php', function(data){ jsonobject=eval(data); day1content = jsonobject.json1; //append div $('#showday1').append(day1content); //create button, add value var input = '<input type="button" class="save_event" value="save program" />'; //attach button $('.event').append(input);
this works fine. want attach event button, ie:
$('.save_event').click(function() { console.log("here"); });
and when place directly below script above nothing happens. know why occuring? need to special jquery mobile?
you should use following:
// new way (jquery 1.7+) - .on(events, selector, handler) $('.event').on('click', '.save_event', function(event) { event.preventdefault(); alert('testlink'); console.log("here"); });
this attach event input within .event
element, reducing scope of having check whole document
element tree , increasing efficiency.
more info here:
Comments
Post a Comment