Reputation: 12138
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
Reputation: 186
You must use jquery data() function like this:
var theActiveData = function() {
return($('ul li.hovering').data("tag"))
}
alert(theActiveData());
Upvotes: 0
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());
Upvotes: 0
Reputation: 1480
you forgot return
var theActiveData = function() {
return $('ul li.hovering').attr("data-tag");
}
alert(theActiveData);
Upvotes: 2