0

I have the following css

<div class="test"></div>

I want to set string "test" in variable like this:

var $stringvalue = $('.test').class(); /*but seems wrong statment*/
4
  • Please clarify. This is really unclear Commented Sep 10, 2013 at 8:11
  • var $stringvalue = "test" should be result Commented Sep 10, 2013 at 8:11
  • 1
    I call it programming with closed eyes. Commented Sep 10, 2013 at 8:15
  • 1
    Don't you notice there is test in $('.test') ? Commented Sep 10, 2013 at 8:16

3 Answers 3

1

You can use the .attr() function.

var $stringvalue = $('.test').attr("class");
  • Notice this will return all the classes that were set in the class attribute on the original DOM.

You can also use the native className property to get an array of class names.

var classes = $('.test')[0].className.split(/\s+/)`
Sign up to request clarification or add additional context in comments.

Comments

1

Use .attr()

var $stringvalue = $('.test').attr('class');

attr('class') will get the class attribute of div with class of test.

1 Comment

@down voter...why.?? can you please explain..
0
var $stringvalue = $('.test').class(); 

should be

var $stringvalue = $('.test').attr('class'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.