SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    826
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    click event not getting triggered (probably a JSON issue....)

    oh man... I have gotten so much help from folks here regardin my JSON issues in the last two days... for which I'm very very grateful, because I have learned a lot...
    I have learned more JSON in the last two days than in all the time I've been trying to learn JSON...;-)

    so am humbly submitting one more question...

    http://mayacove.com/dev/json/topics_lcl.html

    test link at very top gets triggered, it shows all three values I want to show

    but the links where it really should be happening, namely the "topic" links on the left, are simply not getting triggered at all, not even the alert, but I get no errors...

    JS is here, http://mayacove.com/dev/json/js/topics_lcl.js
    I moved the .click-binding code to the very bottom of the document.ready() wrapper, nothing changes....

    what could this be?

    thank you very much....

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,078
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi Maya,

    The problem is that you are inserting the topic links into your page dynamically, so they are ignoring the handlers that are registered when the DOM renders.

    To address this you need to use event delegation, that is, attach the handler to an element that is present when the page loads.

    Something like this:

    Code JavaScript:
    $('body').on('click', '#left a', function(e){
      e.preventDefault();
      alert('test');
      $('section').hide();
      $('section').eq($(this).index('#left a')).slideDown();
    });

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    826
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    oh my gosh, that's right -- the famous "on()".... :-)

    thank you!!!!

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,078
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    No problem.
    Glad it worked


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •