I have a jQuery script which is triggered by an onClick event ( through $(".addContent").click(function(){ ) and which updates the page, adding an input text tag.
I am subsequently trying to trigger another script using an onChange event ( through $(".guestInput").change(function(){ ) on the input text tag which was just added, once the viewer makes a change. It doesn't work.
I have tried adding the input text tag as static html, and the subsequent onChange event fires successfully, but not when I add the field through jQuery.
Hope this is clear.
Any idea as to what I am doing wrong?
Thanks,
John
You need to use live() for DOM elements added after the initial page load.
so .click(function() { do_something; });
would now be .live("click", function() { do_something; });
see: http://api.jquery.com/live/
Description: Attach an event handler for all elements which match the
current selector, now and in the future.
Related
This question is kind of a duplicate, but the answer provided doesn't actually solve the issue. Here's the related question:
Why jQuery click doesn't work when included in a separate file
I've got a php template that has an overlay div I am dynamically populating with different content, depending upon which link is clicked. So for example, I have this in my template:
<a class="icon-search" href="#"></a>
<div id="overlay" class="hidden"></div>
In my global.js file, I have these functions:
$(document).ready(function(){
$("a.icon-search").click(function () {
$("#overlay").load("inc/search.php");
$("#overlay").toggleClass('show hidden');
});
$("#cancel").click(function() {
$("#overlay").toggleClass('show hidden');
});
});
The cancel button is in "inc/search.php"
When you click "icon-search", the overlay toggles properly, and the content of search.php gets loaded, but pressing the cancel button doesn't work, unless I move that function into the search.php file. I really hate doing this, because it makes the html really messy, and it makes reusing things difficult. Is there any way to overcome this issue, so that functions will work on elements that are included?
You need to use event delegation in order to have generated content fire events.
Lookup the .on() method in the jQuery documentation (http://api.jquery.com/on/)
Try this instead:
$(document).ready(function(){
$("a.icon-search").click(function () {
$("#overlay").load("inc/search.php");
$("#overlay").toggleClass('show hidden');
});
$(document).on('click','#cancel',function() {
$("#overlay").toggleClass('show hidden');
});
});
Using .on() will delegate click events on the document to the elements with the specified selector and this will work for all current elements (which are in the DOM when this code runs) and future elements (like those which are appended using AJAX, like when you use the .load() method).
The event handlers like you are using (which I call static event handlers), must be attached directly to the DOM object they are handling events for. That means that when you run the code to attach the event handler, the DOM object you want to attach to must already exist.
If you are running this code on .ready(), but your dynamically loaded content has not yet been loaded, then no event handler will be attached because there's no DOM object yet to attach it to.
There are two general approaches to solving this type of issue:
Run the code AFTER you've loaded the dynamic content (this is what putting the script into your dynamically loaded PHP content does).
Switch to using delegated event handling. In delegated event handling, you attach an event handler to a parent of the dynamic content that is itself not dynamically loaded and thus it already exists. Then, when a click happens on the dynamic content, that click will "bubble" up through it's parents and encounter the click handler you have. jQuery automates a lot of this for you when using the delegated form of .on() which is of this form $("#staticParent").on("click", "#dynamicChild", fn). You can read the details about using the delegated for of .on() in these references:
Does jQuery.on() work for elements that are added after the event handler is created?
jQuery .live() vs .on() method for adding a click event after loading dynamic html
jQuery .on does not work but .live does
I have a page with dynamic content from PHP and I want to use jQuery's load function to periodically refresh the div container which contains the dynamic content so that new content is displayed on top of the old content.
I using a jQuery function as follows:
setInterval(function() {
$("#ContentWrapper").load("Livefeed.php #ContentWrapper");
}, 10000);
to refresh the content container's wrapper every 10 seconds
My problem is that after the load function is executed. Click events and hover events do no properly work on the page. Initially they did not fire at all so I used Jquery live function on all click and hover events within the content wrapper and it solved the problem.
However, when the page reloads the hovers reload as well and all hidden divs with are made visible by jQuery's .show method hide. Is there any way to prevent the hover from reloading and also for the visible hidden divs to remain visible every time the .load function is called.
I'm using click and hover functions like so:
$('selector').live("click",function(){
});
and
$('selector').live("hover",function(){
});
Any help is appreciated.
Using jQuery.live is deprecated now. So don't use it. Use jQuery.on from now on. Also, .hover event is being taken down. You should use separate mouseenter and mouseleave events.
You can fix this issue by replacing .live with:
/*
If click happens inside contentwrapper, check if click target was yourSelector, if true execute this function
*/
$("#ContentWrapper").on("click", "#yourSelector", function(){
//do whatever u wanna do on click
});
To keep track of all divs that were hidden and all those weren't you need to set unique id for each div. And before you reload with .load() you loop through all divs and save their current state (hidden or visible) in an Object Array in [{ id : state },..] format. Once ajax request finishes, loop through Object array via $.map and set states for each div
You can use JQuery delegate,its primary intention is dynamically driven content:
http://api.jquery.com/delegate/
$("#Parent").delegate("selector","click", function() { });
I have my code here.
// HTML Code
<div class="article"></div>
// jQuery
$('.article').append('Toggle div');
Is it possible to use that newly created href link and toggle a div like this:
// jQuery
$('.toggle').click(function() {
$('.article').toggle('slow');
});
I have tried the above with no luck. I just get redirected to index page.
Don't use click(): it's deprecated and wouldn't work even if it weren't. Use on():
$('.toggle').on('click', function() {
$('.article').toggle('slow');
});
Also, change your a tag to Toggle div.
As to the reason why on() works and click() doesn't, you need to read jQuery's documentation. In short, the click event has been bound to an element on the page; this scope does not cover future elements that are added to the DOM. On(), however, is a delegated event:
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, you can use delegated events to
avoid the need to frequently attach and remove event handlers.
I am designing webpage using jquery and php. My page has side menu, and clicking one of the option it send a request to server to read some information from file and it will create a form out of it, with submit and other button edit(in case anybody wants to change the information in that form) and send this html back to client. I am able to do this successfully. But when I click on the edit button it actually not calling the click handler I registered for the all the buttons.
$('button').click(function(){
alert("click event");
});
I included this in the
jQuery(document).ready(function() {
But because all the jquery/js code in ready() and it gets executed at the page load time, its not able to find these buttons in form because its something which i get from server after loading and replacing it to existing 'div' and hence its not able to invoke the event handler. If I define click handler for the parent div of this form, it receives the click event if I click 'edit' button because that 'div' was present when initial page got loaded. I might not be doing it correctly, may be getting the whole form from server is not a good idea, if you have to do some operation on the form at client side. But is it doable? if yes then whats the possible way out?. Thanks!
Your event isn't firing because you define it prior to the element existing on the page. Using the .on() method should fix this. Something along the lines of:
$('body').on('click','button', function(){
alert("click event");
});
should work.
If I understand you correctly you adding the buttons dynamic to the form. You should try to use jQuery.on() insteed, see http://api.jquery.com/on/
And in your example this might work for you
$("body").on("button","click", function(event){
alert("Hello world");
});
Use on (jQuery 1.7 and up). Previously was delegate (1.4.2+), before that live...
$('*your_form*').on('click', 'button', function(){
alert("click event");
});
You may simply need to use this instead:
$(document).on('click','button',function(){
alert("click event");
});
(jQuery 1.7 or higher)
you have to call using button id
$('#buttonid').click(function(){
alert("click event");
});
or button class
$('.buttonclassname').click(function(){
alert("click event");
});
How can I run jquery function for a .click() event on a element created by an ajax request ?
For a normal element it works, but I want to do this for an element created by the ajax request. And it's not working ...
$(".links").click(function(){alert("aaaa");})
a class="links" >1</a;
I've also tried with "links" as id.
I need it for a pagination system.
Please help :(
You need to run the code again to bind the click event handler, after the new element is inserted into the DOM. Alternatively, you may want to use .live().
use jquery live
use jquery live,
$(".links").live('click', function(){
//user code here
});
to deal with dynamically loaded doms, you should use live function.
jquery do not load page doms automatically after page loaded.
You should use live() like this:
$(".links").live('click', function(){alert("aaaa");})
This also works with elements added to document
for an onclick to every <a> element you must do like this (if this is what you mean:
$("a").live('click', function(){
alert($(this).attr("id"));
})
This attaches event to all <a> elements and also to those added afterwards.
EDIT - now the alert shows the id of the link: 'this' in this case refers to the <a> that has been clicked