ass
ass
...">

How to get div id in div in jQuery?

How do you get id in div?

<div id="container">
   <div id="frag-123">ass</div>
   <div id="frag-123">ass</div>
   <div id="frag-123">ass</div>
</div>

Thanks!

+3
source share
4 answers

There are several ways to do this. You can use map():

var ids = $("#container").children().map(function(n, i) {
  return n.id;
});

or each():

$("#container").children().each(function(n, i) {
  var id = this.id;
  // do something with it
});

etc.

+20
source

you can use

attr('id')
+5
source
$('div', $('div#container')).each(function() {
    console.log($(this).attr('id')); 
});
+2
source

to get them as an array of strings

var ids = $.map($('#container div'), function(n,i) {
              return n.id
          });
+2
source

Source: https://habr.com/ru/post/1718160/


All Articles