valerio0999
valerio0999

Reputation: 12138

jquery get a data-tag and do something with it

i have this structure:

<ul>
  <li data-tag="one" class="hovering">something</li>
  <li data-tag="two">something else</li>
  <li data-tag="two">something else</li>
</ul>

what i need is storing in a variable the data-tag value of the <li> that hasClass("hovering") - i want the alert to print "one".

I've been trying all types of different stuff that revolves more or less around this:

var theActiveData = function() {
    $('ul li.hovering').attr("data-tag");
}
alert(theActiveData);

but i just can't get this to work

Upvotes: 0

Views: 73

Answers (3)

ingemi
ingemi

Reputation: 186

You must use jquery data() function like this:

var theActiveData = function() {
return($('ul li.hovering').data("tag"))
}
alert(theActiveData());

Upvotes: 0

spaceman
spaceman

Reputation: 1167

You need to call the function in the alert and set the function to actually return the value.

var theActiveData = function() {
    return $('ul li.hovering').attr("data-tag");
}
alert(theActiveData());

Example

Upvotes: 0

Shiran Dror
Shiran Dror

Reputation: 1480

you forgot return

var theActiveData = function() {
    return $('ul li.hovering').attr("data-tag");
}
alert(theActiveData);

Upvotes: 2

Related Questions