Forum Moderators: open
Is it possible to have several divs, with the same ID. and change them all at once.
When I do this: document.getElementById().innerHTML = text
It will only change the first div, with that id, not them all. Any suggestions on this?
Thanks alot,
Wesley
If you are using prototype then you can do getElementsByClassName. If not, then take a look at this little hack I whipped up below :-)
Cheers!
-------------------------
<html>
<head>
<script language="javascript">
function UpdateDivs(className,replaceHTML) {
var divs = document.getElementsByTagName('DIV');
for (var i=0;i<divs.length;i++) {
var name = ' ' + divs[i].className;
if (name.indexOf(' ' + className)!= -1) {
divs[i].innerHTML = replaceHTML;
}
}
}
</script>
</head>
<body>
<div>I will not change.</div>
<div class="nope">Neither will I</div>
<div class="yep">But I will</div>
<div class="Bluetext yep">And so will I</div>
<button onclick="UpdateDivs('yep','<b>I changed!</b>');">Change Divs</button>
</body>
</html>
-------------------------