get value of an html tag in javascript

-1

hello everyone would like to know how to take the text you have on a label to later use it in uan avariable  hi

apparently with value does not work with .value or .innerHTMl I hope someone helps me with this. greetings

    
asked by Juandev 10.12.2018 в 00:43
source

1 answer

0
  • Value is used to obtain the value of an input.
  • InnerHTML is used to get the html contained within an element.
  • What I think you're looking for is textContent

I'll give you the example so you can see it

var a = document.getElementById('saludos');
document.write("<br><br>");


document.write("Texto impreso con innerHTML (se imprime e interpreta la etiqueta span):");
document.write("<br>");
document.write(a.innerHTML);

document.write("<br><br>");
document.write("Texto impreso con textContent (sólo se recoge el texto):");
document.write("<br>");
document.write(a.textContent);
span {background-color: #AAA;}
<div id="saludos">
Buenos días, <span>buenas tardes</span> y buenas noches.
</div>
    
answered by 10.12.2018 в 01:01