Picking up content from an HTML tag with JS

Asked

Viewed 13,898 times

3

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

<ALGUMATAGHTML> O valor definido arqui</ALGUMATAGHTML>

Since the value I want to capture is:

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

I tried with Javascript but it didn’t work. JS code:

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

Does anyone there know how to take it in JS?

  • I could not understand. What is "Here"? Which element with id "streamurl"?

  • @Dvdsamm answered and removed until the question is edited rs, finger itches to answer.

  • Edit the question by explaining the question better.

  • I just readjusted the question, the problem is that this is my first time in the stack and did not know the restriction of Html, so it ends up disappearing all the code that pasted in the post... Malz

2 answers

5


It depends on what kind of tag you want to.

value - This property returns the value inserted 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 that is inside a container, such as a 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 that is inside, but also the tags who are also.

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

  • Well... For now the code of the first answer has served well for the purpose, now just create one to assemble a url using this information and forces the redirection of the page.

  • To do this, create a new question.

  • I’m trying to create using the site "w3schools.com", if not using the references of la, I will vote for one more question...

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>

  • Vlw man... has somewhere to mark as the right answer?

  • Yes, below the arrow to the left of response.

  • Ready vlw... Good night

  • @Diegoqueiroz Glad you helped! Now, I would recommend not using the variable url because it can give conflict any time with some library or plugin, I don’t know... I also used url in my codes, until one day they stopped working. I just changed to url_that everything worked again, but it took me a lot to go after changing all the url codes on multiple pages. I usually avoid using variables with common names (url is one of them), so I added an underscore.

  • VLW by tip, but this particular code is to work in a Tampermonkey script, specific for the site openload.co, with the aim of converting their player to one in Html5 that I use...

  • @Diegoqueiroz, blz! If it’s working, it’s great. I who stopped using because I always used in my Ajax and never gave problem, until one day Ajax did not work and I discovered that was this variable url. I traded for url_ and spoiled beauty and never again gave problem :)

  • Changing now kkk - Better get used to not using...

  • @Diegoqueiroz rsrs good! It is even better to avoid. Type, for example, give field name in "date" database: although date is a word in Portuguese, it is also an English word that is reserved by many programming languages. Result: will give problem in code rs.

  • I get it... FLW

Show 4 more comments

Browser other questions tagged

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