jquery parent child selecter returning undefined

Viewed 805

Why jquery parent child selector is not working here.

Here article element has it's children element section, and section contains html select tag.

So, with parent child logic, it has to work, isn't it ? It is returning undefined.

$(document).on('change', 'select[name="slct_sec"]', function(){

    //This one is working
    var cls=$('section > select[name="slct_cls"]').val();
    
    //Not working
    var cls_1=$(this).parent('section').siblings('section > select[name="slct_cls"]').val();
 
   //Not working
    var cls_2=$(this).parent('article').children('section > select[name="slct_cls"]').val();
    alert(cls_1);
    alert(cls_2);
 
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
  </head>
  <body>
    <article>
      <section>
        <select name='slct_cls'>
          <option value='1'>One</option>
          <option value='2'>Two</option>
        </select>
      </section>
      <br/>
      <section>
        <select name='slct_sec'>
          <option value='1'>A</option>
          <option value='2'>B</option>
        </select>
      </section>
    </article>
  </body>
</html>

3 Answers

You don't know the section corresponding to the element you want to target, the one with select[name="slct_cls"], so you can't use siblings - with a sibling, you'd want to select the one with the slct_cls, but you don't know which sibling has that beforehand. Better to go one level higher, to the article, and use .find to find a descendant element with that selector:

$(document).on('change', 'select[name="slct_sec"]', function() {
  var cls_1 = $(this).closest('article').find('select[name="slct_cls"]').val();
  console.log(cls_1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
</head>

<body>
  <article>
    <section>
      <select name='slct_cls'>
        <option value='1'>One</option>
        <option value='2'>Two</option>
      </select>
    </section>
    <br/>
    <section>
      <select name='slct_sec'>
        <option value='1'>A</option>
        <option value='2'>B</option>
      </select>
    </section>
  </article>
</body>

</html>

Use $(this).closest('article') to get parent tagged <article> instead of .parent('article').

var cls_1=$(this).closest('article').find('select[name="slct_cls"]').val();

or

$(this).parent().siblings().find('select[name="slct_cls"]').val();

I've changed your code. The problem was on the sibling() usage. The select is not the sibling of the section. Also you should use parents() instead of parent() to get all parents on your query.

$(document).on('change', 'select[name="slct_sec"]', function(){
debugger;
    //This one is working
    var cls=$('section > select[name="slct_cls"]').val();
    
    //Not working
    var cls_1=$(this).parent().siblings('section').find('[name="slct_cls"]').val();
 
   //Not working
    var cls_2=$(this).parents('article').children('section').find('[name="slct_cls"]').val();
    alert(cls_1);
    alert(cls_2);
 
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
  </head>
  <body>
    <article>
      <section>
        <select name='slct_cls'>
          <option value='1'>One</option>
          <option value='2'>Two</option>
        </select>
      </section>
      <br/>
      <section>
        <select name='slct_sec'>
          <option value='1'>A</option>
          <option value='2'>B</option>
        </select>
      </section>
    </article>
  </body>
</html>

Related