-4

I have code like

 var i = $(this).children().eq(0).attr('id');

I want to know that, had I already stored the id of that children div in variable called i. Or I have to write some other codes? If I have to write some other codes what it might be? As I am new to jQuery it will be very helpful if I get any suggestion.

  • 2
    Welcome. Can you please reformulate your question? – Jeto Oct 19 '18 at 13:07
  • LMGTFY Assumptions here: `this` is the DIV reference, you only have one child `.eq(0)` so yes, that gets the id of that first child element IF it has an id property. https://stackoverflow.com/q/3239598/125981 for the id, https://api.jquery.com/children/ - the children. https://api.jquery.com/eq/ the `.eq(0)` – Mark Schultheiss Apr 09 '19 at 12:53

1 Answers1

0

You should get to know the 'each' function in jQuery when iterating over children, its very useful.

Try this:

var parent = $(this); // selector could be anything, like $('#theParentElement')
var childIDs = [];

parent.children().each(function() {

    var child = $(this);
    childIDs.push(child.attr('id'));

    // you can simplify to childIDs.push($(this).attr('id'));
});
Tom_B
  • 307
  • 2
  • 8
  • thank you Tom_B. Can you tell me, in my code did I already store the id of the children in var i? or it isn't stored yet? – web creator Oct 19 '18 at 18:40
  • Happy to help. From my testing you would have only stored the ID of the first child element. If my solution has answered your question can you please mark it ask correct – Tom_B Oct 19 '18 at 19:03