Languages
[Edit]
EN

JavaScript / XPath - get input by name

10 points
Created by:
Eshaal-Wilkinson
774

In this short article we would like to show how to get input element by name attribute using XPath in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <form>
    <label>Username:</label>
    <input name="username" value="John" />
  </form>
  <script>
    
    var result = document.evaluate('//input[@name="username"]', document.body);
    var input = result.iterateNext();
    
    if (input) {
    	console.log(input.value);
    }
    
  </script>
</body>
</html>

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript / XPath

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join