Rick
Rick

Reputation: 273

Jquery load over the top of the previously loaded content

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.

Upvotes: 1

Views: 646

Answers (1)

gregers
gregers

Reputation: 13040

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(){ 
    //...
});

Upvotes: 4

Related Questions