How to Find an Input Element by Value Attribute in JavaScript?

Estimated read time 1 min read

You can use the document.querySelector() method in JavaScript to find an input element by its value attribute.

Here’s an example that demonstrates how to find an input element by its value attribute:

const input = document.querySelector(`input[value="Example"]`);

console.log(input);
/*
Output:
<input type="text" value="Example">
*/

In this example, the document.querySelector() method is used to find the first input element in the document that has a value attribute with the value “Example”. The querySelector() method takes a CSS selector as its argument, and the selector input[value="Example"] matches the first input element that has a value attribute with the value “Example”. If an input element is found that matches the selector, the querySelector() method returns it. If no input element is found, the querySelector() method returns null.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply