0

i wanna read the value of a html span. how i can do this?

here is the code:

//this works
spanRight= document.getElementsByClassName("spanRight")

//but i cant get the text
spanRight = spanRight.text;
<span class="spanRight">1234567890</span>

thank you for your help

letsTry420
  • 11
  • 1
  • 6
  • Use `textContent`. – Walk Oct 19 '17 at 08:22
  • `getElementsByClassName()` returns a node list. Please open your browser console to check for error reports and maybe research the selectors you use to fully understand them, I would also recommend you research the properties you try to access of those elements too... – NewToJS Oct 19 '17 at 08:23
  • textContent returns undefined. – letsTry420 Oct 19 '17 at 08:26

2 Answers2

1

You would use innerText.

spanRight = spanRight.innerText;

There is also innerHTML which can return with HTML tags. Differences are listed here.

Sam Bunting
  • 845
  • 1
  • 15
  • 36
0

use textContent like this Example:

spanRight= document.getElementsByClassName("spanRight")

//but i cant get the text
text = spanRight[0].textContent

alert(text);
<span class="spanRight">1234567890</span>
ferhado
  • 2,363
  • 2
  • 12
  • 35