Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  111] [ 3]  / answers: 1 / hits: 20487  / 13 Years ago, tue, november 15, 2011, 12:00:00

In javascript, how to get a class name from a td cell?



example:



<td class=ColumnHeader style=text-align:right; >


class ColumnHeader is a class inside css, how could i retrieve it from css and changed the width size in javascript?


More From » html

 Answers
392

Select all the <td> elements via getElementsByTagName() and iterate over them looking for the className:



var tds = document.getElementsByTagName(td);
for (var i = 0; i<tds.length; i++) {

// If it currently has the ColumnHeader class...
if (tds[i].className == ColumnHeader) {
// Set a new width
tds[i].style.width = new_width;

// Or set a different class which defines the width
tds[i].className = someOtherClass;
}
}

[#89123] Sunday, November 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quinn

Total Points: 160
Total Questions: 86
Total Answers: 101

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
;