/    Sign up×
Community /Pin to ProfileBookmark

Combine 2 Onloads together

How would I combine these 2 onloads together. Right now they don’t work with each other and gives me an error. Seperate the scripts work fine. Note: They both work off the same <table id=”table1″>

<head>

<script type=”text/javascript”>
onload=function(){
var cells = document.getElementById(‘table1’).getElementsByTagName(‘tbody’)[0].getElementsByTagName(‘td’);
var v=0;
for(var i=4;i<cells.length;i+=5) {
v+= Number(cells[i].firstChild.data);
}
var av = (v*5)/cells.length

document.getElementById(‘average’).firstChild.data=av.toFixed(2)
}
</script>

<body onload=”ezcolrow(‘table1’);”>
<script>
function ezcolrow(el)
{
var ele = document.getElementById(el);
for(x = 1; x < ele.rows.length; x++)
{
ele.rows[x].style.height = “20px”
if (x%2 == 0)
{
ele.rows[x].style.backgroundColor = “#dddddd”;
}
else
{
ele.rows[x].style.backgroundColor = “#FFFFFF”;
}
}
}
</script>
</head>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@felgallSep 28.2006 — The smallest change to get it to work is:

&lt;head&gt;

&lt;script type="text/javascript"&gt;
onload=function(){
[b]ezcolrow('table1');[/b]
var cells = document.getElementById('table1').getElementsByTagName('tbody')[0].getElementsByTagName('td');
var v=0;
for(var i=4;i&lt;cells.length;i+=5) {
v+= Number(cells[i].firstChild.data);
}
var av = (v*5)/cells.length

document.getElementById('average').firstChild.data=av.toFixed(2)
}
&lt;/script&gt;


&lt;body&gt;
&lt;script&gt;
function ezcolrow(el)
{
var ele = document.getElementById(el);
for(x = 1; x &lt; ele.rows.length; x++)
{
ele.rows[x].style.height = "20px"
if (x%2 == 0)
{
ele.rows[x].style.backgroundColor = "#dddddd";
}
else
{
ele.rows[x].style.backgroundColor = "#FFFFFF";
}
}
}
&lt;/script&gt;
&lt;/head&gt;
Copy linkTweet thisAlerts:
@HDCauthorSep 28.2006 — Thanks felgall, it works perfectly!
Copy linkTweet thisAlerts:
@thechasboiSep 28.2006 — HDC

What I have found to be a good way for multiple onload functions is to have a <script> tag at the very end of all the html output like this;
[code=php]<script language="JavaScript" type="text/javascript">
window.onload=function () {libinit();}
</script>[/code]

and the libinit() be in an upper script tag. libinit obviously could be what ever you name it.


function libinit(){

do stuff ........

}

Hope this helps
×

Success!

Help @HDC spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...