WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] jquery append link (2 posts)

  1. carlla
    Member
    Posted 4 years ago #

    hello!

    My new plugin use jquery to make a menu tree. It have to create dinamic elements.

    But, when I create a link element, Jquery ignory the click event. I think the append function is creating text instead html elements.

    I tried the code:

    jQuery(document).ready(function($){  
    
    // the top level menu - click event
    jQuery('.myclass').click(function() {
      var id = $(this).attr('name');
      alert(id);
      $('#mylist-'+id).append("<a class=\"myclass2\" href=\"#\" name=\"test\" > TEST </a>  ");
     }); // end click
    
    // the second level menu - event click
    jQuery('.myclass2').click(function() {
      var id = $(this).attr('name');
      alert(id);
    });
    
    }); //end jquery

    When click in link (class=myclass) I need create a link (class=myclass2) and it need to call a second function when clicked.
    The fist event worked well and create the element. But I didn't get the second alert when click in the test link (class=myclass2).

    Someone have even tried to do something like that?

  2. carlla
    Member
    Posted 4 years ago #

    jQuery's click event seems considers only the first click on page, no matter how many links there are, only first click can run a function. It seems not happen to other html elements, only "a" element.

    So use jQuery's live event instead click event when you are working with "a" elements and need to get the click event.

    I changed

    jQuery('.myclass').click(function() {
    ... });

    to

    jQuery('.myclass').live("click", function() {
    ... });
    });

    Now it works, and I could make my dynamic menu =)

Topic Closed

This topic has been closed to new replies.

About this Topic