/    Sign up×
Community /Pin to ProfileBookmark

find max value using js in table

My programming days are long gone.. didnt do much js but trying to help a friend.

the question “how to find the max value in a html table”
for instance.. I have x number rows with col1 being Name and Col2 being a total. the task is to find the Name with the Max Col2.

I started this but running into problems (in this situation I want to write the value 3 ) :

<html>
<head>
<script type=”text/javascript”>
function changeContent()
{
var max = 0;
var newvalue = 0;
var i = 0;
for (i=0; document.getElementById(‘myTable’).rows; i++)
{ newvalue=document.getElementById(‘myTable’).rows[i].cells

if (newvalue[1] > max)
{ max = newvalue[1];
}
}
document.writeln(max)
}
</script>
</head>

<body>
<table id=”myTable” border=”1″>
<tr>
<td>Row1 cell1</td>
<td>1</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>3</td>
</tr>
</table>
<form>
<input type=”button” onclick=”changeContent()” value=”Change content”>
</form>
</body>

</html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinDec 13.2005 — your middle condition in your for statement is not a test condition

you forgot to put 'i <' so it reads 'i < document.getElementById('myTable').rows'
Copy linkTweet thisAlerts:
@dmlegareauthorDec 13.2005 — Thanks. I missed that.

It appears its still not working. the Writeln is returning 0.

document.getElementById('myTable').rows is this the correct way of getting the number of rows a table?
Copy linkTweet thisAlerts:
@CrazyMerlinDec 13.2005 — rows is an object, so you need to use rows.length

there are 2 cells in each row, so you need to reference the cell that you need to query...in this case cell[1] ...zero-based cell[1] is the second cell. If you do as you are doing, you are passing newvalue the whole object, so you are passing about 30 values that you do not need, on a big table this would cause problems.

to get the value inside the cell, because it's not a textbox we cannot use 'value' so we have to use 'innerText'

so your function will look like this:

[CODE]
function changeContent()
{
var max = 0;
var newvalue = 0;
for (var i=0; i < document.getElementById('myTable').rows.length; i++){
newvalue=document.getElementById('myTable').rows[i].cells[1].innerText;
if (newvalue > max)
max = newvalue;
}
document.writeln(max)
}

[/CODE]
Copy linkTweet thisAlerts:
@CharlesDec 13.2005 — 1) Mark up those table headings with TH elements like you should.

2) Then try something like: &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="Content-Script-Type" content="text/javascript"&gt;
&lt;meta name="Content-Style-Type" content="text/css"&gt;

<i> </i> &lt;script type="text/javascript"&gt;
function getMax(t) {
var e, i = 0, m = Number.MIN_VALUE
while (e = document.getElementById (t).getElementsByTagName ('TD')[i++]) {
m = Math.max (m, e.firstChild.data)
}

<i> </i>var t = document.createTextNode ('Maximim: ' + m)
<i> </i>var p = document.createElement ('P')
<i> </i>p.appendChild (t)
<i> </i>document.getElementsByTagName ('BODY')[0].appendChild (p)
}
&lt;/script&gt;

<i> </i> &lt;style type="text/css"&gt;
table {background-color:#a00}
th, td {background-color:#fff}
&lt;/style&gt;

<i> </i>&lt;/head&gt;
<i> </i>&lt;body&gt;
<i> </i> &lt;table id="myTable" border="1"&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;th&gt;Row1 cell1&lt;/th&gt;
<i> </i> &lt;td&gt;1&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;th&gt;Row2 cell1&lt;/th&gt;
<i> </i> &lt;td&gt;2&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;th&gt;Row3 cell1&lt;/th&gt;
<i> </i> &lt;td&gt;3&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;/table&gt;
<i> </i> &lt;div&gt;&lt;button onclick="getMax('myTable')"&gt;Max&lt;/button&gt;&lt;/div&gt;
<i> </i>&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @dmlegare 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.7,
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,
)...