-4

I have a use case where I need to select first input element with in each div and these divs are cotained in a parent div like:

<div class="parent">
  <div>
    <input id="field1" " type="text" value="" name="fieldName1">
    <input id="field2" " type="text" value="" name="fieldName2">
  </div>
  <div>
    <input id="field3" " type="text" value="" name="fieldName3">
    <input id="field4" " type="text" value="" name="fieldName4">
  </div>
  <div>
    <input id="field5" " type="text" value="" name="fieldName5">
    <input id="field6" " type="text" value="" name="fieldName6">
  </div>
</div>

Update: I tried $(".parent :input"); but it gives me all input fields

I need to select all input element whose names are like fieldName1,fieldName3 and fieldName5 Any idea will be helpful.

ABC
  • 4,263
  • 10
  • 45
  • 72

2 Answers2

3

DEMO

$('.parent > div').each(function(){
    alert($(this).find('input').first().attr('name'));
});
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
0

You could try this:

$.each($(".parent div"), function(index,item) {
    $.each($(":nth-child(1)", this), function(index,item){
        console.log($(this).attr("name"));
    });
});
acontell
  • 6,792
  • 1
  • 19
  • 32