Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CSS class name in JavaScript

Tags:

javascript

css

I am facing a problem to get the class name from a string in JavaScript.

For example:

var ddd="<p class='Box_title'>Heading text here...</p>";

Now from that I want to get p tag's class name.

like image 240
Anil Avatar asked Apr 26 '26 10:04

Anil


1 Answers

Browsers are good in HTML parsing:

//setup
var tmp = document.createElement('div');
tmp.innerHTML = ddd;

// get the class
var class_name = tmp.children[0].className;
like image 163
Felix Kling Avatar answered Apr 29 '26 00:04

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!