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*/
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

3 Answers3

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+/)`
Itay
  • 16,601
  • 2
  • 51
  • 72
1

Use .attr()

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

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

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0
var $stringvalue = $('.test').class(); 

should be

var $stringvalue = $('.test').attr('class'); 
Engineer
  • 5,911
  • 4
  • 31
  • 58