Question

I have a a table which has dynamic td's i want to change the weight of letters when clicked. following is my coding but it changes the whole page when clicked. Please tell me where I got it wrong.

function addHandler()
{
    var addH=document.getElementsByTagName('td');       
    for( var i=0;i < addH.length;i++)
    {
        if(addH[i].addEventListener)
        {
            addH[i].addEventListener('click',addBold,false);
        }
        else if(addH[i].attachEvent)
        {
            addH[i].attachEvent('onclick',addBold);
        }
    }
}

function addBold()
{   
    var add=document.getElementsByTagName('td');        
    for( var i=0;i < add.length;i++)
    {
        var weightVal=add[i].style.fontWeight;
        if(weightVal!=900)
        {           
            add[i].style.fontWeight="900";
        }
        else
        {
            add[i].style.fontWeight="100";
        }
    }           
}
Was it helpful?

Solution

Your addBold routine is setting ALL td's. The following works. I wish I could give you a reference link, but I can't. I want that link myself.

[edit] This page works like I think you want it to. Does it work for you?
The "e" gets passed to the event handler. I wish I could say more.
Like I said, I'm looking for a reference on how this works. But it does.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
</style>
<script type="text/javascript">
function addHandler() {
  var addH=document.getElementsByTagName('td');       
  for( var i=0;i < addH.length;i++) {
    if(addH[i].addEventListener) {
      addH[i].addEventListener('click',addBold,false); }
    else if(addH[i].attachEvent) {
      addH[i].attachEvent('onclick',addBold); } }
  }
function addBold(e) {
  if (e.target.style.fontWeight != "900") {
    e.target.style.fontWeight = "900"; }
  else {
    e.target.style.fontWeight = "100"; }
  }
</script>
</head>
<body onload="addHandler();">
<div id="bodyid">
<table border="1">
<tr><td>One1</td><td>Two1</td></tr>
<tr><td>One2</td><td>Two2</td></tr>
</table>
</div><!-- bodyid -->
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top