/    Sign up×
Community /Pin to ProfileBookmark

Combining to javascripts

I’ve been trying to figure out a way to combine two JavaScript’s that are both used with table id. I don’t know much about coding, so have not been successful in my attempt. Don’t even know if this is possible or not, hopefully it is. Could somebody give me a hand?

Here are the two scripts:

[CODE]function tafla(uppl)
{
var ele = document.getElementById(uppl);
for(x = 0; x < ele.rows.length; x++)
{
ele.rows[x].style.height = “20px”
if (x%2 == 0)
{
ele.rows[x].style.backgroundColor = “#FDFBED”;
}
else
{
ele.rows[x].style.backgroundColor = “#F1EFE1”;
}
}
}[/CODE]

Script #2:

[CODE]function rowCount(countWord){

var table = document.getElementById(‘tbl’);
var rows = table.getElementsByTagName(‘tr’);
var avoid = [0];// Separate numbers by commas. e.g. [0,5]

function inArray(arr,fval){
for(var i = 0; i < arr.length; i++){
if(arr[i] == fval)
return true;
}
return false;
}

var cellCount = new Array();
for(var i=0;i<rows.length;i++){
var cells = rows[i].getElementsByTagName(‘td’);
for(var x=0;x<cells.length;x++){
var re = new RegExp(countWord,”gi”);
if(cellCount[x] == null){cellCount[x] = 0;}
if(re.exec(cells[x].innerHTML) != null){
cellCount[x] += 1;
}
}
}

var totalsRow = document.createElement(‘tr’);
for(var i=0;i<cellCount.length;i++){
var td = document.createElement(‘td’);
if (!inArray(avoid,i))
td.innerHTML = cellCount[i];
totalsRow.appendChild(td);
}

table.tBodies[0].appendChild(totalsRow);

}

window.onload = function(){
var lidtvo;
if( lidtvo=window.location.search.match(/[?&]lidtvo=(w+)/) )
var telja=lidtvo[1];
var divide=telja.replace(/(S)([A-Z])/g,’$1 $2′);
rowCount( divide ); //Pass the words which you want to count, seperate by pipe
}[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMay 11.2008 — Do you have access to the HTML that will utilize the two scripts?

Also, see if this helps: http://javascript.internet.com/snippets/multiple-onload-2.html
Copy linkTweet thisAlerts:
@marringiauthorMay 12.2008 — Yes I have access to the HTML. I tried to work with the onload script you pointed me to, but with no success.
Copy linkTweet thisAlerts:
@JMRKERMay 12.2008 — Yes I have access to the HTML. I tried to work with the onload script you pointed me to, but with no success.[/QUOTE]

Well I don't. How about posting it so we can see where the JS is being called and how you are doing it?
Copy linkTweet thisAlerts:
@marringiauthorMay 14.2008 — Well I don't. How about posting it so we can see where the JS is being called and how you are doing it?[/QUOTE]

Hi, the fallowing is the html:

[CODE]<html>
<head>
<title> </title>
<script type="text/javascript">
function table(load)
{
var ele = document.getElementById(load);
for(x = 0; x < ele.rows.length; x++)
{
ele.rows[x].style.height = "20px"
if (x%2 == 0)
{
ele.rows[x].style.backgroundColor = "#FDFBED";
}
else
{
ele.rows[x].style.backgroundColor = "#F1EFE1";
}
}
}
</script>
<script type="text/javascript">
function rowCount(countWord){


var table = document.getElementById('loadtable');
var rows = table.getElementsByTagName('tr');
var avoid = [0];// Separate numbers by commas. e.g. [0,5]

function inArray(arr,fval){
for(var i = 0; i < arr.length; i++){
if(arr[i] == fval)
return true;
}
return false;
}

var cellCount = new Array();
for(var i=0;i<rows.length;i++){
var cells = rows[i].getElementsByTagName('td');
for(var x=0;x<cells.length;x++){
var re = new RegExp(countWord,"gi");
if(cellCount[x] == null){cellCount[x] = 0;}
if(re.exec(cells[x].innerHTML) != null){
cellCount[x] += 1;

}
}
}

var totalsRow = document.createElement('tr');
for(var i=0;i<cellCount.length;i++){
var td = document.createElement('td');
if (!inArray(avoid,i))
td.innerHTML = cellCount[i];
totalsRow.appendChild(td);
}

table.tBodies[0].appendChild(totalsRow);

}

window.onload = function(){
var lidtvo;
if( lidtvo=window.location.search.match(/[?&]lidtvo=(w+)/) )
var telja=lidtvo[1];
var divide=telja.replace(/(S)([A-Z])/g,'$1 $2');
rowCount( divide ); //Pass the words which you want to count, seperate by pipe
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1" width="400" id="loadtable">
<tr>
<th width="100">New Field</th>
<th width="100">First Field</th>
<th width="100">Second Field</th>
<th width="100">Third Field</th>
</tr>

<tr>
<td width="100">Two</td>
<td width="100">word</td>
<td width="100">Many Words On</td>
<td width="100">Many Words On</td>
</tr>

<tr>
<td width="100">Two words</td>
<td width="100">Two</td>
<td width="100">Many Words On</td>
<td width="100">Many Words On</td>
</tr>

<tr>
<td width="100">Two words</td>
<td width="100">Word</td>
<td width="100">Many Words On</td>
<td width="100">Many Words On</td>
</tr>

<tr>
<td width="100">Two words</td>
<td width="100">Video</td>
<td width="100">Two words</td>
<td width="100">Many Words On</td>
</tr>

<tr>
<td width="100">Many Words On</td>
<td width="100">Many Words On</td>
<td width="100">Two words</td>
<td width="100">Many Words On</td>
</tr>

<tr>
<td width="100"> </td>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
</tr>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@JMRKERMay 14.2008 — While I'm not sure what your HTML is trying to do, I would suggest looking into:

  • 1. Useing the word 'table' as a function name in 'rowcount'.

    I don't think it is advisable to use a reserved word like that.


  • 2. I don't see where you are using the function 'tafla()' at all in the HTML


  • 3. There are also some syntax errors you should try to correct in the HTML


  • 4. I see only one onload() function, not 2 as you asked about in post #1.


  • Or maybe I just confused ... ?
    Copy linkTweet thisAlerts:
    @marringiauthorMay 14.2008 — I'm just looking for a way to combine these two scripts into one script so that they can both be used on the same table. I don't think you fully understand me. Thank you though for the help
    ×

    Success!

    Help @marringi 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 5.19,
    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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...