简体   繁体   中英

How can I get the value of an HTML tag using JS

Okay guys, I do have another thread related to this one, however this is a new question.

I have this code in my HTML page:

<span class="licon liconspan">1</span>

Now how can I get the value of "1" into a JS variable?

Actually "1" is not a value of a tag, it is its content. In its turn, it is stored in innerHTML property of a certain element. For your particular case, you can access it like this:

var variable = document.querySelector('.liconspan').innerHTML;

you must write the index [0] because the method document.getElementsByClassName return an array

var element = document.getElementsByClassName(".liconspan")[0];
var variable = element.innerHTML;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM