I have a Dynamic table that I want to give color to on alternative rows. How can I achieve this with css? I need the code to work in IE7+
Asked
Active
Viewed 5,450 times
1
-
Which language / framework are you using? – Stefan Jul 16 '12 at 14:38
-
In JSP.. I'm iterating Dynamic table.. – Mr.Chowdary Jul 16 '12 at 14:45
-
none of the answers given will work on ie8 apart from the jquery solution. Even that comes with the caveat that it will not work on dynamically loaded content unless you do a bit more work and fire the two statements after the content is loaded – Nicholas King Jul 16 '12 at 14:53
-
I'm using IE 7... How to handle it.? – Mr.Chowdary Jul 16 '12 at 15:02
4 Answers
3
Look into using even/odd rules in CSS3.
Reference: https://developer.mozilla.org/en/CSS/:nth-child
For instance,
tr:nth-child(odd)
will represent the CSS for every 2n + 1
child, whereas tr:nth-child(even)
will represent the CSS for every 2n
child.

Christian Benincasa
- 1,215
- 1
- 21
- 45

Daniel Li
- 14,976
- 6
- 43
- 60
2
CSS3 nth-child selector:
tr:nth-child(odd) {
background: red /* or whatever */;
}

chipcullen
- 6,840
- 1
- 21
- 17
2
You can use a CSS3 selector:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
or jQuery:
$("tr:even").css("background-color", "#CCC");
$("tr:odd").css("background-color", "#FFF");
or do it on the server side.
-
This is for class or id? how to use it when many tables are there? But i want to apply for only one table.. – Mr.Chowdary Jul 16 '12 at 14:56
-
This is for all table rows. You can assign a class to the table `
` and use it in your CSS: `table.striped tr:nth-child(even) {background: #CCC}`
– Stefan Jul 16 '12 at 15:03 -
@Nicholas King is telling that it is not working.. I tried in my code. Really not working.. Any alternative? – Mr.Chowdary Jul 16 '12 at 15:11
-
-
In jQuery, how to add class or id in the calling statement what you have given? – Mr.Chowdary Jul 16 '12 at 15:18
-
-
2
i came across this same problem Friday, i used the jquery solution of
$("tr:even").css("background-color", "#CCC");
$("tr:odd").css("background-color", "#FFF");
- a stack overflow solution .js posted here Detect changes in the DOM
so essentially you add the .js script in the head and fire the jquery rules on dom change.
My finished .js looked like this
<script type="text/javascript">
(function (window) {
var last = +new Date();
var delay = 100; // default delay
// Manage event queue
var stack = [];
function callback() {
var now = +new Date();
if (now - last > delay) {
for (var i = 0; i < stack.length; i++) {
stack[i]();
}
last = now;
}
}
// Public interface
var onDomChange = function (fn, newdelay) {
if (newdelay)
delay = newdelay;
stack.push(fn);
};
// Naive approach for compatibility
function naive() {
var last = document.getElementsByTagName('*');
var lastlen = last.length;
var timer = setTimeout(function check() {
// get current state of the document
var current = document.getElementsByTagName('*');
var len = current.length;
// if the length is different
// it's fairly obvious
if (len != lastlen) {
// just make sure the loop finishes early
last = [];
}
// go check every element in order
for (var i = 0; i < len; i++) {
if (current[i] !== last[i]) {
callback();
last = current;
lastlen = len;
break;
}
}
// over, and over, and over again
setTimeout(check, delay);
}, delay);
}
//
// Check for mutation events support
//
var support = {};
var el = document.documentElement;
var remain = 3;
// callback for the tests
function decide() {
if (support.DOMNodeInserted) {
window.addEventListener("DOMContentLoaded", function () {
if (support.DOMSubtreeModified) { // for FF 3+, Chrome
el.addEventListener('DOMSubtreeModified', callback, false);
} else { // for FF 2, Safari, Opera 9.6+
el.addEventListener('DOMNodeInserted', callback, false);
el.addEventListener('DOMNodeRemoved', callback, false);
}
}, false);
} else if (document.onpropertychange) { // for IE 5.5+
document.onpropertychange = callback;
} else { // fallback
naive();
}
}
// checks a particular event
function test(event) {
el.addEventListener(event, function fn() {
support[event] = true;
el.removeEventListener(event, fn, false);
if (--remain === 0) decide();
}, false);
}
// attach test events
if (window.addEventListener) {
test('DOMSubtreeModified');
test('DOMNodeInserted');
test('DOMNodeRemoved');
} else {
decide();
}
// do the dummy test
var dummy = document.createElement("div");
el.appendChild(dummy);
el.removeChild(dummy);
// expose
window.onDomChange = onDomChange;
})(window);
$(document).ready(function () {
$("tr:even").css("background-color", "#CCC");
$("tr:odd").css("background-color", "#FFF");
onDomChange(function () {
$("tr:even").css("background-color", "#CCC");
$("tr:odd").css("background-color", "#FFF");
});
});
</script>
I would like to caveat this answer that this probably is not the greatest solution but worked for what i needed it to do. :-)

Community
- 1
- 1

Nicholas King
- 938
- 7
- 22
-
Hi Nicholas,Here messages is id? how to define it in html with table? – Mr.Chowdary Jul 16 '12 at 15:24