Languages
[Edit]
EN

JavaScript / XPath - get input by name

10 points
Created by:
Eshaal-Wilkinson
354

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>

 

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