Forum Moderators: open

Message Too Old, No Replies

Changing Mutiple Div IDs at once?

document.getElementById("") for several?

         

kazisdaman3

5:40 am on Mar 5, 2007 (gmt 0)

10+ Year Member



Hi there, appreciate you taking your time to help already.

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

ericjust

5:56 am on Mar 5, 2007 (gmt 0)

10+ Year Member



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>

-------------------------

kazisdaman3

6:07 am on Mar 5, 2007 (gmt 0)

10+ Year Member



Ekks I'm having trouble on both! appreciate the help

on your function
function UpdateDivs(className,replaceHTML) {

what do I put in className, the <div class="test">

UpdateDivs("test",replaceHTML)?

kazisdaman3

6:57 am on Mar 5, 2007 (gmt 0)

10+ Year Member



Thank you,

it works!

much appreciated!