JavaScript jquery parent child selecter returning undefined jquery parent child sibling,jquery parent child selector,jquery parent

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>
Answer:1

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>
Answer:2

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();
Answer:3

I have this body div(ng-class="toggle ? 'nav-open' : 'nav'") .container .logo img(src='images/logo.png') span Motto ul(ng-class="toggle ? 'menu-open' : ...

I have this body div(ng-class="toggle ? 'nav-open' : 'nav'") .container .logo img(src='images/logo.png') span Motto ul(ng-class="toggle ? 'menu-open' : ...

I am still somewhat new to JavaScript, Node, etc. and come from a Groovy background (which has extremely similar syntax to JavaScript). In Groovy, I can easily do something like this: def myMap1 = {}...

I am still somewhat new to JavaScript, Node, etc. and come from a Groovy background (which has extremely similar syntax to JavaScript). In Groovy, I can easily do something like this: def myMap1 = {}...

  1. overwrite javascript object
  2. javascript overwrite object properties
  3. javascript overwrite object in array
  4. javascript overwrite object with another
  5. javascript overwrite object function
  6. javascript overwrite object value
  7. javascript overwrite object method
  8. javascript overwrite object property value
  9. js overwrite object properties
  10. js overwrite object
  11. js overwrite object function
  12. js overwrite object in array
  13. js overwrite object value
  14. overwrite window object javascript
  15. javascript overwrite json object
  16. javascript object overwrite key

I am trying to create customized, togglable tabs in wordpress, but I am stuck. I would like to change the title part of the tabs to images. Something like that, but in wordpress: http://jsfiddle.net/...

I am trying to create customized, togglable tabs in wordpress, but I am stuck. I would like to change the title part of the tabs to images. Something like that, but in wordpress: http://jsfiddle.net/...

  1. wordpress customize tabs

I'm trying to add a class name to a child component, preserving the original classes that may be set to the component. Here is the code: import React, { Component } from "react"; import PropTypes ...

I'm trying to add a class name to a child component, preserving the original classes that may be set to the component. Here is the code: import React, { Component } from "react"; import PropTypes ...