Get content from an HTML tag with JS

1

Well ... my doubt is how to capture the value that lies between:

<ALGUMATAGHTML> O valor definido arqui</ALGUMATAGHTML>

The value I want to capture is:

<span style="" id="streamurl">Aqui!!!</span>

I tried with JavaScript but it did not work. Code in JS:

var url = document.getElementById('streamurl').value;
alert(url);

Does anyone know how to handle this in JS?

    
asked by anonymous 01.10.2017 / 02:42

2 answers

1

It will depend on what kind of tag you want.

  

value - This property returns the value entered in form fields, such as inputs and checkboxes, etc.

var url = document.getElementById('streamurl').value;
alert(url);
<input type="text" size="60" id="streamurl" value="http://google.com" />
  

innertText and text - These two properties return the text inside a container, such as div , span , etc.

var url = document.getElementById('streamurl').text;
alert(url);

var url = document.getElementById('streamurl').innerText;
alert(url);
<a id="streamurl" href="#">pt.stackoverflow.com</a>
  

innertHTML - This property returns not only the text inside but also the tags tags that are too.

var url = document.getElementById('streamurl').innerHTML;
alert(url);
<a id="streamurl" href="#">Link 2 <i>Tag</i></a>
    
01.10.2017 / 02:50
0

Use .innerHTML instead of .value :

var url = document.getElementById('streamurl').innerHTML;
alert(url);

var url = document.getElementById('streamurl').innerHTML;
    alert(url);
<span style="" id="streamurl">Aqui!!!</span>
    
01.10.2017 / 02:56