thotheolh
thotheolh

Reputation: 7450

Changing HTML id via JS

I hava a script that consist of "hello world" and the "hello" and "world" are in two different CSS styles.

I would like to click onto either of them and they would swap their styles. Example is I click on "hello" and it would swap style with "world". Below is my codes.

I couldn't get it to swap. How should I correct it ?

<html>
<head>
<style>
#today{
    font-weight: bold;
    color: red;
}

#normal{
    font-weight: normal;
    color: green;
}
</style>
<script>
    old="old";

        function set(in){
            var e = document.getElementsByName(old);
            for(ii=0;ii<e.length; ii++)
            {
                var obj = document.getElementsByName(old).item(ii);
                obj.id="normal";
            }
            old=in;
    var e = document.getElementsByName(old);
            for(ii=0;ii<e.length; ii++)
            {
                var obj = document.getElementsByName(old).item(ii);
                obj.id="today";
            }
        }
</script>
</head>
<body>
<table>
    <tr>
        <td id="normal" name="new" onclick="set('new')">Hello</td>
        <td id="today" name="old" onclick="set('old')">World</td>
    </tr>
</table>
</body>
</html>

Upvotes: 0

Views: 88

Answers (2)

phihag
phihag

Reputation: 287775

At any time, only one element can be annotated with an ID. Therefore, you should use classes instead of IDs to annotate your elements. Also, make sure to include a doctype.

Live demo of the corrected code:

<!DOCTYPE html>
<html>
<head>
<style>
.today{
    font-weight: bold;
    color: red;
}

.normal{
    font-weight: normal;
    color: green;
}
</style>
<script>
document.addEventListener("DOMContentLoaded", function() {
  var els = document.querySelectorAll('.normal,.today');
  for (var i = 0;i < els.length;i++) {
    els[i].addEventListener('click', function () {
      var els = document.querySelectorAll('.normal,.today');
      for (var i = 0;i < els.length;i++) {
        var currentClass = els[i].getAttribute('class');
        var newClass = currentClass == 'today' ? 'normal' : 'today';
        els[i].setAttribute('class', newClass);
      }
    }, false);
  }
}, false);

</script>
</head>
<body>
<table>
    <tr>
        <td class="normal">Hello</td>
        <td class="today">World</td>
    </tr>
</table>
</body>
</html>

Of course, it's way easier to write with jQuery (demo):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script>
<script>
$(function() {
  $('.normal,.today').click(function() {
    $('.normal,.today').each(function(i, el) {
      var $el = $(el);
      if ($el.hasClass('today')) {
        $el.removeClass('today');
        $el.addClass('normal');
      } else {
        $el.removeClass('normal');
        $el.addClass('today');
      }
    });
  });
});
</script>

Upvotes: 4

dnuttle
dnuttle

Reputation: 3830

You have a few problems here. First, in your onclick handler, it has to be "javascript:set" instead of just "set". Second, you have a parameter named "in" which is a Javascript keyword. Change all references of it to "inv". If you do that, your code will sort-of work. Try it.

Beyond that, I suggest that you work in Firefox, and get the Firebug add-in, so that you can open the console. It will show you these kinds of errors.

Upvotes: 1

Related Questions