3

How to get the id from the inside div. I have many div with class control but I want to get parent div id. How to get?

http://jsfiddle.net/cngt2ef6/7/

$(".getid").click(function() {
   alert($(this).closest('div').hasClass("control).attr("id"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control" id="spa">
  Content
</div>
<div class="control" id="test">
  <div class="method">
    <a href="#" class="getid">Getid</a>
  </div>
</div>
<div class="control" id="awesome">
  Content
</div>

2
  • 6
    Change .closest('div').hasClass("control) to .closest('div.control') Commented Sep 19, 2018 at 9:40
  • You can use .parents() method of jQuery. $(this).parents(".control").attr("id"); Commented Sep 19, 2018 at 10:03

2 Answers 2

5

The problem comes from :

  1. The closest selector that will return the first parent div (what is not what we need here).
  2. The hasClass() since it will return a boolean if the element has the given class or not. (in your case the first parent doesn't have the given class control)

Instead of using hasClass() you need to use the class selector directely in the closest() method like:

$(this).closest('div.control').attr("id");

$(".getid").click(function() {
  console.log($(this).closest('div.control').attr("id"));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control" id="spa">
  Content
</div>
<div class="control" id="test">
  <div class="method">
    <a href="#" class="getid">Getid</a>
  </div>
</div>
<div class="control" id="awesome">
  Content
</div>
Js:

Sign up to request clarification or add additional context in comments.

1 Comment

@ Zakaria:Can you answer this question:stackoverflow.com/questions/52424524/…
1

Besides closest() - which return the first single node parent - already mentioned you can use parents() , but in this case will return multiple nodes with the element/class/id targeted.

It is just an alternative to closest() in this case scenario

$(".getid").click(function() {
  console.log($(this).parents('.control').attr("id"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control" id="spa">
  Content
</div>
<div class="control" id="test">
  <div class="method">
    <a href="#" class="getid">Getid</a>
  </div>
</div>
<div class="control" id="awesome">
  Content
</div>

6 Comments

You can, but it's irrelevant here - and you should indicate that .closest() finds the single parent that is closest while .parents() could return multiple nodes.
In this case there is no multiple nodes therefore no need to mention that I think :)
closest() is preferred when searching for a single ancestor, as it stops after the first match.
Future question: I got this answer from ^^here^^ and it states I can use .parents() as an exact replacement for .closest() - why am I getting multiple results? Just be complete.
Nice alternative solution :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.