View Full Version : Javascript function

07-08-2012, 11:40 PM
I am newbie into javascript and i need one function. For example i have 4 divs (div1, div2, div3, div4) and i want to give one specific css class (css1) randomly in only two divs when fired pageload event.
Thnx in advance.

Philip M
07-09-2012, 08:36 AM
This assigns the classname css1 to two divs randomly on page load.


<body onload = "assignClass()">

<div id = "div1">Content of Div1</div>
<div id = "div2">Content of Div2</div>
<div id = "div3">Content of Div3</div>
<div id = "div4">Content of Div4</div>

<script type = "text/javascript">
function assignClass() {
var thedivs = ["div1", "div2", "div3", "div4"];
var shuffled = shuffle(thedivs);
document.getElementById(shuffled[0]).className ="css1";
document.getElementById(shuffled[1]).className ="css1";
alert (shuffled[0] + " classname is " + document.getElementById(shuffled[0]).className); // for testing
alert (shuffled[1] + " classname is " + document.getElementById(shuffled[1]).className); // for testing

function shuffle(Arr) {
var NewArr = Arr.slice(0); // copy the array
var len = NewArr.length;
while (len>0) {
return NewArr;



At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global
warming is wrong.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum