tags:

views:

61

answers:

4

I try to write these code to make grid view through CSS:( jsbin )

   var tables = document.getElementsByClassName('tableData');
    var rows = tables[0].getElementsByTagName('tr');
for(var i=1; i<rows.length; i +=2) {
  alert(rows[i]);
  rows[i].className = "alt";
}

This only works with the tr elements has class="" . But if I want to add class to tr . I've tried Core.addClass but it doesn't work .

+4  A: 

Try rows[i].className += " alt";

SLaks
Nice and simple, thanks so much :)
nXqd
A: 

This should work even when the attribute isn't already present:

rows[i].setAttribute("class", "alt");

http://www.w3schools.com/Dom/met_element_setattribute.asp

Eric Mickelsen
One of us is misunderstanding the question. I believe it's you.
SLaks
You could be right.
Eric Mickelsen
@SLaks: I think that you both are a little wrong in different ways. tehMick, I think it should be `rows[i].setAttribute("class", rows[i].getAttribute("class") + " alt");`
voyager
@voyager: Yeah, I think I agree. The snippet you gave is probably just the ticket.
Eric Mickelsen
+1  A: 

You might want to use a Javascript framework. It turns out that Firefox and Internet Explorer both have different approaches for adding class names.

For Firefox, I believe you have to do

element.setAttribute('class','<className>');

while in IE, you can do

element.className='<className>';

EDIT

It turns out that Firefox supports the element.className attribute. However, Internet Explorer doesn't understand element.setAttribute('class'...). You have to do element.setAttribute('className'...) if you want to use the setAttribute function call.

jQuery provide an interface where you can do

$(element).addClass('<className>');

and it takes care of all the browser ambiguities. There are also removeClass() and hasClass() functions for managing and testing class attributes.

So in your scenario, you'd do:

var tables = document.getElementsByClassName('tableData');
var rows = tables[0].getElementsByTagName('tr');
for(var i=1; i<rows.length; i +=2) {
    // alert(rows[i]);
    $(rows[i]).addClass("alt");
}

Note that you could simplify this further with more jQuery functionality.

cmptrgeekken
hit the nail on the head.
Joseph Silvashy
`element.className = "name";` works correctly on Firefox.
voyager
The method setAttribute will work in IE as well. It's standard, not specific to Firefox. Also, className is supported by Firefox, not just IE.
Eric Mickelsen
@Joseph, noticed your first comment about not suggesting a specific Javascript framework. I ended up showing an example for jQuery simply because I thought the answer would benefit from one such example. Do you think it'd be better to show examples from multiple frameworks, or would it be better to leave it as it is?
cmptrgeekken
@cmptrgeekken: I'd leave it as it is. Adding another framework example would just make your answer longer without any real gain in information, as most frameworks have very similar methods.
voyager
yup, jQuery is really great. But I want to learn basic first so :pThanks for your comment :)
nXqd
+1  A: 

This will only add the class name if it doesn't already exist

var classToAdd = 'alt';
if (rows.className.length == 0)
{
  rows.className = classToAdd;
}
else if (rows.className.indexOf(classToAdd) < 0)
{
  rows.className += ' ' + classToAdd;
}