/    Sign up×
Community /Pin to ProfileBookmark

Howto set all cells classname in a table

I’m trying to highlight a cell in a table. Previous I want to set all other cells back to normal. Cant get it to work. Came up with this code:
——————————

<html>
<head>

<style type=”text/css”>
<!–
.tedit {
border: 1px dotted;
}
.teditHL {
border: 1px dotted #FFCCCC;
}
–>
</style>

<script type=”text/javascript”>
function setMe(myCell){
var xRows=document.getElementById(‘myTable’).rows.length;
for(x = 0; x < xRows; x++){
var xCells=document.getElementById(‘myTable’).row[x].cells.length;
for(y = 0; y < xCells; y++){
document.getElementById(‘myTable’).row[x].cell[y].className=’tedit’;
}
}
myCell.className = “teditHL”;
}
</script>
</head>

<body>
<table cellpadding=”10″ cellspacing=”10″ width=”400″ height=”400″ id=”myTable”>
<tr>
<td class=”tedit” onClick=”setMe(this)”>edit</td>
<td class=”tedit” onClick=”setMe(this)”>edit</td>
</tr>
<tr>
<td class=”tedit” onClick=”setMe(this)”>edit</td>
<td class=”tedit” onClick=”setMe(this)”>edit</td>
</tr>
</table>
</body>

</html>
—————————————–

can’t get it to work. Can anyone please tell me what i’m doing wrong here? Any solution or suggestion would be more than welcome.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JonaDec 12.2004 — [font=trebuchet ms]I've written an article on this just recently: <http://www.webreference.com/programming/javascript/jf/column7/index.html>. Is this what you need?[/font]
Copy linkTweet thisAlerts:
@CharlesDec 12.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<meta name="Content-Style-Type" content="text/css">

<title>Example</title>

<style type="text/css">

<!--

td {border:solid 1px}

td.highlight {background-color:#aaa}

-->

</style>

<script type="text/javascript">

<!--

function setMe (cell) {

var e, i = 0;

while (e = document.getElementById ('myTable').getElementsByTagName ('TD')[i++]) {e.className = ''};

cell.className = 'highlight';

}

// -->

</script>

</head>

<body>

<table cellpadding="10" cellspacing="10" width="400" height="400" id="myTable">

<tr>

<td class="tedit" onclick="setMe(this)">edit</td>

<td class="tedit" onclick="setMe(this)">edit</td>

</tr>

<tr>

<td class="tedit" onclick="setMe(this)">edit</td>

<td class="tedit" onclick="setMe(this)">edit</td>

</tr>

</table>

</body>

</html>[/font]
Copy linkTweet thisAlerts:
@weemauthorDec 12.2004 — Jona,that's a pretty interesting article... thanks for sharing it with me. But for my project I'll go with the solution of Charles. I like the simplicity of the code. It just does what I want it to do.

thanks for your solutions.
×

Success!

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