Jquery is not working on newly added elements to the dom with Plugin like JRate Plugin
You need to append the html element first so that it is registered in the DOM
. Then, you can call jRate
on it
var divjRate = "<div><div class='jRate'></div></div>";
// Append new element to container of choice
$(divjRate).appendTo('.fb-jRate');
// Use plugin on new element
$('.jRate').jRate({
onSet: function(rating) {
alert(rating);
}
});
dynamically added dom-elements not responding to jQuery-function
If you are dynamically adding elements to the DOM and expect to be attaching events to them, you should consider using event delegation via the on()
function :
// This will wire up a click event for any current AND future 'td' elements
$(table1).on('click', 'td', function(){
$(this).html("");
});
Simply using click()
on it's own will only wire up the necessary event handlers for elements that exist in the DOM at the time of that function being called.
Event binding on dynamically created elements?
As of jQuery 1.7 you should use jQuery.fn.on
with the selector parameter filled:
$(staticAncestors).on(eventName, dynamicChild, function() {});
Explanation:
This is called event delegation and works as followed. The event is attached to a static parent (staticAncestors
) of the element that should be handled. This jQuery handler is triggered every time the event triggers on this element or one of the descendant elements. The handler then checks if the element that triggered the event matches your selector (dynamicChild
). When there is a match then your custom handler function is executed.
Prior to this, the recommended approach was to use live()
:
$(selector).live( eventName, function(){} );
However, live()
was deprecated in 1.7 in favour of on()
, and completely removed in 1.9. The live()
signature:
$(selector).live( eventName, function(){} );
... can be replaced with the following on()
signature:
$(document).on( eventName, selector, function(){} );
For example, if your page was dynamically creating elements with the class name dosomething
you would bind the event to a parent which already exists (this is the nub of the problem here, you need something that exists to bind to, don't bind to the dynamic content), this can be (and the easiest option) is document
. Though bear in mind document
may not be the most efficient option.
$(document).on('mouseover mouseout', '.dosomething', function(){
// what you want to happen when mouseover and mouseout
// occurs on elements that match '.dosomething'
});
Any parent that exists at the time the event is bound is fine. For example
$('.buttons').on('click', 'button', function(){
// do something here
});
would apply to
<div class="buttons">
<!-- <button>s that are generated dynamically and added here -->
</div>
Jquery Event Not Triggering for DOM Elements Created after page load
Currently what you are using is called a direct binding which will only attach to element that exist on the page at the time your code makes the event binding call.
You need to use Event Delegation using .on() delegated-events approach, when generating elements dynamically or manipulation selector (like removing and adding classes).
i.e.
$(document).on('event','selector',callback_function)
Example
$(document).on('click', '.score', function(){
//Your code
alert("clicked me");
});
In place of document
you should use closest static container.
The delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, we can use delegated events to bind the click event to dynamically created elements and also to avoid the need to frequently attach and remove event handlers.
Jquery click function is not working for dynamic elements
The click()
function you can call on the elements which have direct binding. Direct binding will only attach event handler which are present at the time of DOM loading i.e. static elements.
If there are elements created after DOM is loaded, then not all the events associated with them would be triggered if you have not attached the event handlers correctly.
And when you create dynamic elements that means they are being created after DOM is loaded and they were not present at the time of direct binding, so you can not call directly click()
on that.
if you want to get click
functionality on dynamic created elements, you'll have create a delegated binding by using on. This you can achieve by adding a .on
handler to a static parent element.
Delegated events have the advantage that they can process events from descendant elements that
are added to the document at a later time.
Change this line
$("#questlist_item_button_reward" + obj.questid).click(function() {
alert("reward");
});
to
$("#call_questitem").on("click", "#questlist_item_button_reward" + obj.questid, function() {
alert("reward");
});
And do the same for the go
button as well.
DEMO
Related Topics
Ipad/iPhone Browser Crashing When Loading Images in JavaScript
Getting How Many People Are in a Chat Room in Socket.Io
What's a Good Tool to Screen-Scrape with JavaScript Support
Why Isn't This Object Being Passed by Reference When Assigning Something Else to It
Display the Contents of a Log File as It Is Updated
How to Create a Custom Error in JavaScript
How to Beautify JavaScript Code Using Command Line
How Does the Messages-Count Example in Meteor Docs Work
How to Replace the Entire HTML Node Using Jquery
How to Sort/Order Keys in JavaScript Objects
Thickness of Lines Using Three.Linebasicmaterial
What Are Alternatives to Extjs
Copy to Clipboard in Chrome Extension
Updating an Input's Value Without Losing Cursor Position
Nested Element (Web Component) Can't Get Its Template
How to Test If a Letter in a String Is Uppercase or Lowercase Using JavaScript
How to Wrap an Await/Async Try/Catch Block to Every Function