0

Say I have this:

<div id="container">
    <div>
        <div class="main" data-id="thisID">
        </div>
    </div>
</div>

How do I get the parameter result inside data-id if I do a class search?

var el = $('#container');
var row = el.find(".main");

Then after this, not sure how to use "row" to get the string id from data-id.

Keith
  • 4,059
  • 2
  • 32
  • 56

1 Answers1

4

Use jQuery .data() or Element.dataset

var el = $('#container');
var row = el.find(".main");
var id = row.data("id");

var el = $('#container');
var row = el.find(".main");
var id = row[0].dataset.id;
guest271314
  • 1
  • 15
  • 104
  • 177
  • this didn't get the data-id string, data-id is a parameter name – Keith Dec 29 '16 at 21:05
  • @Keith _"this didn't get the data-id string, data-id is a parameter name"_ Not certain what you mean? Are you trying to get the name of the `data-*` attribute at the element? – guest271314 Dec 29 '16 at 21:06
  • data-id is an attribute that I use. I need to grab that string attribute based on the class "main". There are other attributes within that same class so pulling "id" doesn't pull anything – Keith Dec 29 '16 at 21:07
  • @Keith _"There are other attributes within that same class so pulling "id" doesn't pull anything"_ Is actual `html` different from `html` at Question? Are you trying to get the name or the value of the `data-*` attribute? Or, are you trying to select the element using the `data-*` attribute? – guest271314 Dec 29 '16 at 21:09
  • i'm trying to get the value of the data-id attribute – Keith Dec 29 '16 at 21:20
  • @Keith _"i'm trying to get the value of the data-id attribute"_ See `console` at https://jsfiddle.net/u8umqzpr/ – guest271314 Dec 29 '16 at 21:28
  • 1
    i found what i was doing wrong. I misunderstood at first. Thanks for the help! – Keith Dec 29 '16 at 21:38