0

Lets say I have this...

<div class="container" id="continingStuff">
    <div class="someClass" id="notLookingForThis" data="dataValue1">
        Stuff
    </div>
    <div class="someClass" id="lookingForThis" data="dataValue2">
        Stuff
    </div>
</div>

and I want to get the id of the div thats data value is dataValue2, how can I do that with jQuery or Javascript?

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • This is not a question, and has no code, no further investigating. Please, try to read something about Jquery selectors and read the guide for creating good questions on StackOverflow. Selectors -> https://api.jquery.com/category/selectors/ https://stackoverflow.com/help – Alejandro Teixeira Muñoz Sep 07 '17 at 13:39
  • It technically is a question. It does have markup code and this is me investigating. I didn’t know what a "jQuery selector” was, so how was I to know that I should be reading about that, without asking someone first? You see, going onto google and typing this question in whilst not having a mental model of the scenario is pointless. That is why this site exists, check out their motto. It is a good question because it clearly states the outcome I desire. I outline a scenario and clearly outlined what I want as a result. But thank you very much for taking the time to try and raise your stock. –  Sep 07 '17 at 15:49
  • Refering to https://stackoverflow.com/help/on-topic: Some questions are still off-topic, even if they fit into one of the categories listed above: Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. – Alejandro Teixeira Muñoz Sep 07 '17 at 15:52
  • What if you can’t even start and have no idea how to even start solving a problem? Should people like that just not even bother trying to ask questions? And again, stop trying to raise your stock by using me, this isn’t “homework”, “mate”. –  Sep 07 '17 at 15:55
  • mate, it´s easier for me to gain points answering your question, but I´m just trying to help you learning how to use the site. Googling your question , this is the second result: Get a div id from a div that has a specific data value -> https://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value Just exposing what seems to be a no further work instead of asking. – Alejandro Teixeira Muñoz Sep 07 '17 at 15:57
  • I know how to use the site. I have asked questions before and those questions have included examples of my efforts. This question had no examples because I had no idea how to even start. Have a nice day –  Sep 07 '17 at 15:58

3 Answers3

0

Using jQuery, you can use this selector:

var id = $("div[data='dataValue2']").attr('id');

Here's working a code snippet:

var id = $("div[data='dataValue2']").attr('id');
console.log(id);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" id="continingStuff">
    <div class="someClass" id="notLookingForThis" data="dataValue1">
       Stuff
    </div>
    <div class="someClass" id="lookingForThis" data="dataValue2">
       Stuff
    </div>
</div>
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
0

You can use Attribute Equals Selector to get element, then you can use attr() method to get id of the element.

Example:

$(document).ready(function() {
  var id = $('div[data=“dataValue2”]').attr('id');
  console.log(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=“container” id=“continingStuff”>

 <div class=“someClass” id=“notLookingForThis” data=“dataValue1”>
  Stuff
 </div>

 <div class=“someClass” id=“lookingForThis” data=“dataValue2”>
  Stuff
 </div>

</div>
Kacper Polak
  • 1,411
  • 15
  • 24
0

you can use this for your code

var id = $('*[data="dataValue2"]').attr('id');

rEjITh
  • 79
  • 5