0

I have a box with two dropdownlist . box content change when second dropdown change. box contain ul with lot of li , i want when clicked on li get li content . this do for first time when page load , but after box content change , my script not run.


this is my code (when dropdown change and box content change):

 $.ajax({url: '/Symfony/web/app_dev.php/linknews?s='+target+'&&c='+select,
           success: function(output) {
              var list = document.getElementById(news_list);
              var line = document.getElementById("line");
              $(list).empty(); 

          var newss = JSON.parse(output);
          newss.forEach(function(entry){

            var link = document.createElement("a");
            var linkcontent = document.createTextNode(entry["title"]);
            link.appendChild(linkcontent);
            link.title = entry["title"];
            link.href = entry["address"];

            var div = document.createElement("div");
            div.innerHTML += '&nbsp';
            div.setAttribute("class" , "news-bottom");

            var li = document.createElement("li");
            li.appendChild(link);
            li.appendChild(div);

            list.appendChild(li);
            $(line).show();
          });     
       }
});

and code that show to me li content:

    $(document).ready(function($)
      $("#news-list li a").click(function() {
      var name = this.getAttribute('href');
      alert(name);
}));

i don't know when box content change , why my script not run?!

1 Answers1

0

My question have two aspect:

first: Cross-browser onload event and the Back button , and answer: cross-browser

second: I should use on instead of click: $(document).ready(function(){

$('#news-list').on('click','a',function(){

    alert($(this).attr('href')); // Get the ref attribute from the "a" element
});
$(window).bind("unload", function() {});

});

attention that : Method on was introduced in jQuery version 1.7.

Community
  • 1
  • 1