0

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

7
  • what exactly do you want from it? Your question is not clear Commented May 3, 2016 at 15:17
  • 2
    var theActiveData = $('ul li.hovering').data('tag'); is what you're looking for, I believe. Commented May 3, 2016 at 15:18
  • @RajaprabhuAravindasamy i want the alert to print "one" Commented May 3, 2016 at 15:19
  • var theActiveData = function() { return $('ul li.hovering').attr("data-tag"); } alert(theActiveData()); Commented May 3, 2016 at 15:20
  • Why are you wrapping the variable assignment in an anonymous function call? var theActiveData = $('ul li.hovering').data('tag'); Commented May 3, 2016 at 15:20

3 Answers 3

2

you forgot return

var theActiveData = function() { return $('ul li.hovering').attr("data-tag"); } alert(theActiveData); 
Sign up to request clarification or add additional context in comments.

1 Comment

Also the () to execute the function inside alert
0

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

Comments

0

You must use jquery data() function like this:

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

2 Comments

i want the alert to print "one", but not to set the value. thanks anyway :)
ok I misunderstood your question, I edited my answer for what you need, you was having some wrong sentences in your code, check mine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.