Arvind
Arvind

Reputation: 1

is it possible to getElementsByClassName that only selects <input> tags?

I have html code wherein the same class name is mapped to multiple HTML elements. Out of which I want only to get the value of "input" elements. Below is the sample code, please suggest how to get the value of "input" elements with the class name.

<!DOCTYPE html>
<html>
    <body>
        <div class="example">Element1</div>
        <div class="example">Element2</div>
        <input class="example" value="input1"/>
        <input class="example" value="input2"/>
    </body>
</html>

I tried with below script, but document.getElementsByClassName("example") will return collection of "example" class name including "div" elements. I just need "input" element values.

<script>
const collection = document.getElementsByClassName("example");
collection[0].value = "Hello World!";
</script>

Upvotes: 0

Views: 28

Answers (0)

Related Questions