-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
  • 2
    Welcome. Can you please reformulate your question? Commented Oct 19, 2018 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. stackoverflow.com/q/3239598/125981 for the id, api.jquery.com/children - the children. api.jquery.com/eq the .eq(0) Commented Apr 9, 2019 at 12:53

1 Answer 1

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')); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.