Reputation: 129
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?
Upvotes: 2
Views: 54
Reputation: 1072
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> \ ');
}
Upvotes: 1
Reputation: 1082
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>
Upvotes: 0
Reputation: 30700
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.
Upvotes: 2