Parent> child notation in jquery 1.3

I am using jquery version 1.3

<div id='mainPanel'>
   <div>   
     <h3>Head #1 <a href="#"><input type="text" value="Type 1"/> </a></h3>

     <div>
        <div id="panel_1">
          <div class="items">
             <div><input type='text' value="0"/></div>
             <div><input type='text' value="1"/></div>
             <div><input type='text' value="2"/></div>
             <div><input type='text' value="3"/></div>
        </div> 
      </div>
     </div>

   <div>   
     <h3>Head #2 <a href="#"><input type="text" value="Type 2"/> </a></h3>
     <div>
        <div id="panel_2">
          <div class="items">
             <div><input type='text' value="0"/></div>
             <div><input type='text' value="1"/></div>
             <div><input type='text' value="2"/></div>
             <div><input type='text' value="3"/></div>
        </div> 
      </div>
     </div>

   <div>
</div>

Now, here I want to access the text field values ​​from the chapter div and I need the identifier ie panel_1 and panel_2

so for this I wrote the following code

  $("#mainPanel > div > h3").each(function(index) {

        var panelId = $(this).attr('id'); // i.e. panel_1 and all

        // parent > child notation
        var ele = $(this).next('div > div > div > div').each(function(index){ 


           alert($(this).children('input').val());


});

});

Here I was not able to get the result using parent> child notation

HOW CAN I ACCESS H3> A> INPUT value here

+3
source share
3 answers

Elements <h3>do not contain elements at all <div>, so the first selector will be ruined. In fact, there is no need for it to be so complex:

$('#panel_1 input').each(function() { ...

You will get all input elements in the panel "panel_1".

edit — , , , " ":

$('div.mainPanel div.items').each(function() {
  var containerId = $(this).closest('div[id]').attr('id');
  $(this).find('input').each(function() {
    var anInput = $(this);
    // ...
  });
});

, . "" "id" - , .

+4

$(this).next('div > div > div > div').each(function(index) 

$(this).next('div').find('div > div > div >input').each(function(index) 
0

So, you are given an identifier and need to find the inputs contained in its panel?

 $('#panel_' + givenId + ' input').each(function() { ...

or do you need to go through all the panels and get input?

0
source

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


All Articles