charu charu - 3 months ago 10
HTML Question

How to get class name through JavaScript?

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

Like:

<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

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; }
            else
            {
                elementName = elementName + "," + dd[i].name;
            }
        }