tags:

views:

307

answers:

1

Having issues trying to select html injected into the DOM using jquery's load. Works if ul#links content isn't replaced, otherwise it fails to load into #content.

JS

$(document).ready(function() { 

  // Load links into tab 
  $("ul#tabs li a").click(function(){ 
      urlTabs = $(this).attr("href"); 
      console.log(urlTabs); 
      $("ul#links").load(urlTabs); 
      return false; 
  }); 

  // Load content 
  $("ul#links li a").click(function(){ 
    urlLinks = $(this).attr("href"); 
    console.log(urlLinks); 
    $("#content").load(urlLinks); 
    return false; 
  }); 

});

HTML

  <ul id="tabs"> 
    <li><a href="/tab1">Tab 1 </a></li> 
    <li><a href="/tab2">Tab 2</a></li> 
  </ul> 
  <ul id="links"> 
    <li><a href="/link1">This will load into #div, the others will not</a></li> 
  </ul> 
  <div id="content"> 
    <p>Load content in here</p> 
  </div>

New to js in general, any help appreciated.

+4  A: 

When you're binding the click handler for "ul#links li a", it only binds it to existing DOM nodes matching the selector. The live events will also bind to elements injected dynamically after you do the binding.:

// Load content 
$("ul#links li a").live("click" ,function(){ 
    //...
});
gregers
+1 for beating me to it
karim79
Perfect - thank you!
Rick