Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:51:35+00:00 2026-05-27T04:51:35+00:00

I am using PHP and jquery. I am also using Drupal but I think

  • 0

I am using PHP and jquery. I am also using Drupal but I think this is general problem and probably not related to Drupal.

I have link on a page:

<a href="/my-module/js/1" class="launch-js">Link</a>

Clicking the link launches a snippet of jquery wich fades in some html.

if (Drupal.jsEnabled) {
  $(document).ready(function(){
    $('a.launch-js').click(function(){
    ...

The html which is being faded in also contains

<a href="/my-module/js/1" class="launch-js">Link</a>

But clicking on this link doesn’t work anymore as I expected. The browser loads page /my-module/js/1 instead of updating the current page.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:51:36+00:00Added an answer on May 27, 2026 at 4:51 am

    When you load new content, it overwrites your dom element, and since the event is attached to a particular element, it disappears along with the element. One standard solution to this is to use jQuery.delegate() to attach the event to an element higher up on the page, like this:

    $('body').delegate('a.launch-js', 'click', function(ev) {
        // same code as your function above
    });
    

    This works because events in the DOM “bubble”. When you click on an element, it sends a click event to that element as well as every one of it’s ancestors all the way to the top (assuming no custom code stops the propagation). So you can just attach the event handler to an ancestor of the element, and the have the event handler itself determine if the click came from an element you specified. jQuery hides the details of this and provides a nice simple delegation method to do this for you.

    And in jQuery 1.7, this method has been superceded by jQuery.on():

    $('body').on('click', 'a.launch-js', function(ev) {
        // same code as your function above
    });
    

    Note the I’ve used body as the selector here, but you can use any selector that is an ancestor of all possible a.launch-jses and will always exist. The deeper in the dom (or put another way, the closer it is, as an ancestor, to the final target elements — a.launch.js in this case), the more efficient it is, because the less dom traversal has to be done to check for the event.

    UPDATE: Hat tip to @Clive, who points out in the comments that Drupal 6 only supports up to version 1.3 of jQuery. Neither of the above methods (on or delegate) existed in version 1.3. Instead there was a method called jQuery.live() to accomplish this task, and it worked like this:

    $('a.launch-js').live('click', function(ev) {
        // same code as your function above
    });
    

    This method has a few performance drawbacks that the other methods do not (see the api docs for specific details if you’re interested), and it’s also deprecated, so only use it if you have to (i.e. if you’re using jQuery version 1.3 and can’t upgrade).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how I would achieve this using PHP, but with jQuery / JavaScript
I'm using this jQuery to render a text box onClick. But, It's not rendering...
After loading a PHP template (using jQuery's load function), this simple script won't make
hello i'v built a search, using MySQL, PHP and jQuery, this is the search
For the following project I will be using PHP and jQuery. I have the
I am creating a file tree using PHP & jQuery, but I'm having trouble
I have an ajax heavy web-application built using PHP, jQuery. I am looking for
I have built a CMS system using jQuery and PHP, it has worked consistently
I have a chat box using jQuery and php. setInterval( function(){ if($('#session_container').children().length > 0){
I am using PHP and jQuery to build an interactive timeline which needs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.