2

HTML:

<div class="takeIDfromhere">400788381251</div>

JavaScript:

<script>
    var ID = document.getElementsByClassName('.takeIDfromhere');
    for(var i=0; i < ID.length; i++) { 
        document.write('\<div class="testclass">'+ID[i]+'</div> \ ');
    }
</script>

My point is, JavaScript must run when document ready.
This did not work.

Where is the mistake?

2
  • Put JS <script> tag at the end of body (before </body>). Commented Oct 14, 2014 at 16:08
  • javascript runs when ever put them, not after document loaded. you can run javascript after document loaded using jQuery: $(document).ready(function () { //Code }) Commented Oct 14, 2014 at 16:12

3 Answers 3

2

It's the stray period.

var ID= document.getElementsByClassName('takeIDfromhere')

This should work. But your document.write code might not do what you think it's doing. The \ characters don't make sense to me, nor does the way you're using ID[i]. Try replacing it with document.write("test") or something and go from there.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the . (period) from your class selector and if you want to get the actual ID make sure you are grabbing the innerHTML.

var ID= document.getElementsByClassName('takeIDfromhere');
for(var i=0; i < ID.length; i++) { 
  console.log('\<div class="testclass">'+ID[i].innerHTML+'</div> \ ');
}

Comments

0

Put tag at end of the body or handle on document ready event

 <script>
document.ready = function() {var ID= document.getElementsByClassName('.takeIDfromhere');
for(var i=0; i < ID.length; i++) { 
  document.write('\<div class="testclass">'+ID[i]+'</div> \ ');
}
     }
 </script>

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.