I have a question regarding jQuery. I am really new with jQuery, or even javascript for that matter, so please bear with me.
I am using jQuery UI draggable on some divs in my app. The problem is, jQuery .on('click') doesn't seem to work with these divs. I did try to use .click() and it does work. However, the divs are going to be created dynamically, and based on what I've learned it is best to use .on() when we want to use dynamically created elements. Below is my HTML snippet:
<div class="book-page">
<div class="draggable-text">
<p>First text</p>
</div>
<div class="draggable-text">
<p>Second text</p>
</div>
</div>
And here is the jQuery snippet:
$('div.draggable-text').draggable({
cursor: 'move',
delay: 200,
containment: 'parent'
}).resizable().selectable();
// This one doesn't work
$('div.book-page').on('click', 'div.book-page > div.draggable-text', function () {
alert('clicked!');
// some more coding here...
});
// This one works
/*$('div.book-page > div.draggable-text').click(function () {
alert('clicked!');
// some more coding here...
});*/
And lastly, here is a link to a jsfiddle I've created. I really appreciate any kind of help.
Thanks! And sorry for the bad English.
Jquery Syntax is incorect try this:
Check here : http://jsfiddle.net/jS8sK/2/
for Divs that was created dynamically its better to use
.live
for events.Looks like you have some syntax errors with your second one.
Try this:
Read the syntax for the jQuery 'on' event to understand better what arguments go in. http://api.jquery.com/on/
Cheers!
Try this:
If you want bind click event to dinamically created draggable element, your first idea was almost correct.
Here is a fixed code : http://jsfiddle.net/G75qs/3/