-2

I'm trying to get the input by its name, then get its value, but this isn't working.

token = document.getElementsByName("__RequestVerificationToken").value
console.log(token)

The token:

<input name="__RequestVerificationToken" type="hidden" value="LXRZObY_ZLZ5pIjUd7fGgWONkAqJvXD6queBT3pod0ofwCXG2IhtEywSuaJOGVsjUIvKPyW60irAgP_fKslonLMFww41">

2 Answers2

0

the method "getElementsByName" returns an array so you have to chose the item in the array:

token = document.getElementsByName("__RequestVerificationToken")[0].value
console.log(token)
lem2802
  • 1,152
  • 7
  • 18
0

You just need to add [0] to select your element value:

token = document.getElementsByName("__RequestVerificationToken")[0].value
alert(token)
<input name="__RequestVerificationToken" type="hidden" value="LXRZObY_ZLZ5pIjUd7fGgWONkAqJvXD6queBT3pod0ofwCXG2IhtEywSuaJOGVsjUIvKPyW60irAgP_fKslonLMFww41">
Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35