innerHTML not working with classname in JS

21,717

You ar creating a nodeList (a special array of Nodes) using getElementsByClassName. Alternatively you can use document.querySelector, which returns the first element with className .catlink:

function showDiv( discselect ) {
    if( discselect === 1)    {
      document.querySelector(".catlink").innerHTML = "aaaaaaqwerty";
    }
}
Share:
21,717
swapnesh
Author by

swapnesh

#SOreadytohelp In love with MEAN & MERN stack, ex was LAMP. Feel free to contact me at- [email protected]

Updated on June 01, 2020

Comments

  • swapnesh
    swapnesh almost 4 years

    My drop down List to select particular value-

    <select name="category" id="category" onChange="showDiv(this.value);" >
        <option value="">Select This</option>
        <option value="1">Nokia</option>
        <option value="2">Samsung</option>
        <option value="3">BlackBerry</option>
        </select>
    

    This is the div where i want to show the text

    <span class="catlink"> </span>

    And this is my JS function -

        function showDiv( discselect )
        {
    
        if( discselect === 1)
        {
        alert(discselect); // This is alerting fine
        document.getElementsByClassName("catlink").innerHTML = "aaaaaaqwerty"; // Not working
        }
    
    }
    

    Let me know why this is not working, and what i am doing wrong?