Skip to main content
gamedev.net
🔒 Locked

[web] JQuery Question

Started by Cygnus_X 3 replies 1k views
Original Post
Cygnus_X
Cygnus_X
I have the following (working) JQuery script;

$(document).ready(function(){  $('.link_click').click(function(){      $("#filecontainer").load("script_deletecdfile.php", {proj: $(this).attr('id')} );      return false;  });});


and the cooresponding html:
								

Basically, by clicking 'delete', I delete a record out of my mysql database and re-list all links (records) inside the filecontainer div (script_deletecdfiles.php does the deleting, and echos the revised list). It works great on the first click.... but fails to recognize any future clicks. I'm sure this has to do with replacing the contents of filecontainer which calls my jquery function, but I'm not even sure how to start googeling for a work around to my problem. Thanks.
swiftcoder
swiftcoder
Quote:
Original post by Cygnus_X
I'm sure this has to do with replacing the contents of filecontainer which calls my jquery function.
You only add the &#106avascript callbacks in the document's onready callback, which is only called once, immediately after the page loads.<br/><br/>When you reload the div with new links, these haven't ever had the onclick callbacks added - so you need to add them immediately after loading the updated content.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
btower
btower
You can use the "live" method instead of the "click" method to enable the click to take place on elements added after the page has loaded.

Replace your code with the following to achieve the desired result:

$(document).ready(function(){  $('.link_click').live('click', function(){      $("#filecontainer").load("script_deletecdfile.php", {proj: $(this).attr('id')} );      return false;  });});


Note: there are currently some issues with this approach. IIRC it has been reported to be not working in Chrome/Safari.
Cygnus_X
Cygnus_X
.live worked perfectly. ratings++;

I'll have to see if I get any complaints about the chrome/safari issue.
leiavoia
leiavoia
Quote:
Original post by btower
Note: there are currently some issues with this approach. IIRC it has been reported to be not working in Chrome/Safari.


I haven't noticed any personally. (I should probably double check though, just in case.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.