简体   繁体   中英

How to retrieve data from html page using html class?

<div class="MyName">
  majedur
</div>

I want majedur through using MyName

document.getElementsByClassName("MyName") will return you collection of element that has class "MyName", you can retrieve required by it's index

var elements= document.getElementsByClassName("MyName");

if(elements.length > 0){
     var value = elements[0].innerHTML;
}

try this

var divText = document.getElementsByClassName('MyName')[0].innerHTML;

alert(divText);//for alert the inner text  of the div

console.log(divText);//write in console the inner text  of the div

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