0

i'm trying to get content of div named 'topage' , i did this code

<script type="text/javascript">

       var topagge = document.getElementsByClassName("topage");
             console.log(topagge);

         </script>

in the console it shows like this :

HTMLCollection[1] 0: span.topage ... innerText:"mycontentofdiv" length: 1 proto: HTMLCollection

i want to get the innerText value , i try this code :

 console.log(topagge.innerText);

but in the console it's undefined

please how can i get content of this collection html object ( using the attribut 0 : span.topage wich contain the innerText.

3
  • have you tried topagge.innerHtml? Commented Jul 14, 2015 at 0:44
  • Also check because you have topagge and topage. As said innerHTML is the way to go. Commented Jul 14, 2015 at 0:55
  • even with innerHtml it's stil undefined Commented Jul 14, 2015 at 7:58

5 Answers 5

2
<script type="text/javascript">
    var topagge =document.getElementsByClassName("topage")[0].innerHTML;
    console.log(topagge);
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

This function getElementsByClassName return Collection or return an array of all elements of this class name so you should change your code to be like this

<script type="text/javascript">
    var topagge = document.getElementsByClassName("topage")[0];
    console.log(topagge.textContent); // or topagge.innerHTML;
</script>

Comments

0

You need to wait until the HTML docuemnt is fully loaded. Try this:

<script type="text/javascript">
window.onload = function(){
    var topagge = document.getElementsByClassName("topage");
    console.log(topagge);
}
</script>

EDIT:

window.getElementsByClassName returns an array of elements; then, following code will get HTML content of first element:

topagge[0].innerHTML

1 Comment

yes this is the solution , the problem of undefined is that HTML dosnt complet rendring , but with window.onload it work :) ,
0

To get the content of a div, use .innerHTML.

document.getElementByClassName is not supported in IE. Can you assign the div an ID, and use document.getElementById instead?

2 Comments

i'm using chrome, and even changing by an id it didnt work
document.getElementById is supported by all browsers that run JavaScript
0

Try:

console.log(topagge.innerHTML)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.