Javascript Vs className

I am currently learning javascript and I found some difficulty understanding the difference between class and className.

I am wondering if there is any difference between class and className. When I learn Javascript I found that I can use any of these to get the class name for a specific node: for example

   var firstChildClass = firstChildName.className; 
   var firstChildClass2 = firstChildName.getAttribute('class')

      

A more detailed script can be found at http://jsfiddle.net/hphchan/3ze3ug7r/ .

I would like to ask in implementation, are there any subtle differences between the two?

Btw I visited object.className or object.getAttribute ("className / class" ") and realized that using getAttribute ('class') is more versatile than className as className only works on HTML and not others like SVG But are there any additional differences between the two?

Thank.

+3


source to share


1 answer


As @ Derek- ๆœ• ๆœƒ pointed out, is getAttribute

used to retrieve the value of a class attribute when directly accessing the attribute className

, which allows you to get and set the class names.



Furhtermore, the function getAttribute

works on any type of attribute and can be used to quickly retrieve values โ€‹โ€‹dynamically, eg getAttribute(attributeName)

.

0


source







All Articles