charu charu - 1 year ago 57
HTML Question

How to get class name through JavaScript?

How do I get the class name through JavaScript using element name from classic HTML.


<input name="xxx" class="CheckBox" onchange="CheckOnChange()"
type="CHECKBOX" checked="" value="Y">

I want to check the fields which has the class name "Checkbox" .

How can I solve this? Please help me.

Answer Source

I just reversed my question to get the required output. Here by reading the class name,I am getting the element name using the below JS:

     var elementName = "";
        var dd = document.getElementsByClassName("CheckBox");
        for (i = 0; i <= dd.length - 1; i++) {
            if (elementName == "")
            { elementName = dd[i].name; }
                elementName = elementName + "," + dd[i].name;