Is it possible to get the value of a div?

Asked

Viewed 58 times

2

If in the div valor0 have a value, have I get this value in a variable to sum it for example??

In this example we have the div valor0 with value 1, I can send it to test variable?

var teste = document.getElementById("valor0").value;
console.log(teste);
<html>
	<div id="valor0"> 1 </div>
</html>

1 answer

7


You can get the .textContent and then parse to convert to Numero.

var text = document.getElementById("valor0").textContent;
var numero = Number(text);
console.log(typeof text, text, '||', typeof numero, numero);
<div id="valor0"> 1 </div>

  • Thanks for your help!! @Sergio

  • Sergio, sorry to bother you, this way has already worked, but I would like to know to study, there is another way to return the value of the div??

  • @Sora you can use i innerHTML but if you have HTML it will bring "trash" that you don’t need. You had problems with the answer code, or you have different HTML?

  • So, is that I used this method to take the value of a div and returned the error: Uncaught Typeerror: Cannot read Property 'textContent' of null at ...

  • From what I saw, this error is due to the fact that the script does not find the div or something like

  • @Sora exact. This may be a similar problem to this https://answall.com/q/134810/129 Otherwise I advise you to ask a question, or put a jsFiddle here with an example I can help you with.

  • Thank you @Sergio !!!!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.