EN
JavaScript / XPath - get input by name
10 points
In this short article we would like to show how to get input element by name attribute using XPath in JavaScript.
Quick solution:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<form>
5
<label>Username:</label>
6
<input name="username" value="John" />
7
</form>
8
<script>
9
10
var result = document.evaluate('//input[@name="username"]', document.body);
11
var input = result.iterateNext();
12
13
if (input) {
14
console.log(input.value);
15
}
16
17
</script>
18
</body>
19
</html>