0

i m trying to include some items in the JavaScript arrays that is the id's of the li element. any suggestions.

  <html>
  <head>
  <script type="text/javscript">
   var arr=[];
   </script>
  </head>
   <body>
  <ul>
  <li id="index.html"></li>
  <li id="gallery.html" ></li>
  <li id="Contact Us.html"></li>
  </ul>
  </body>

  </html>

3 Answers 3

2
$(function() {
    var arr = $('li').map(function() {
        return this.id;
    }).get();
});
1
$(function(){
    var arr = [];
    $('li').each(function(){
      arr.push(this.id);
    });
    console.log(arr);
});
0
$(document).ready(function() {
    var arr = new Array();
    $('li').each(function() {
        arr.push($(this).attr('id'));
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.