Jump to content

setting Class for cells inserted by insertCell() ?


fee

Recommended Posts

What I do is clear my table with (excluding the header):

while(table.rows.length>1) {    table.deleteRow(1)      }

and then I want to add some new rows with cells by:

var material = x.insertCell(0)var rodzaj = x.insertCell(1)var ilosc = x.insertCell(2)material.innerHTML = "jeden"rodzaj.innerHTML = "dwa"ilosc.innerHTML = "trzy"

I wonder if i can set class="someclassname" for new cells using DOM properties? Or is there other way/trick to do it?

Link to comment
Share on other sites

You can assign a class by using className:

<style type="text/css">.no_tint {  background-color: #fff;}.tint {  background-color: #6ff;}</style><script type="text/javascript">var i = 0;function insRow(){var c;((++i) % 2 == 0 )? c = 'no_tint' : c = 'tint'; var x=document.getElementById('myTable').insertRow(0);x.className = c;var y=x.insertCell(0);var z=x.insertCell(1);y.innerHTML="NEW CELL1";z.innerHTML="NEW CELL2";}</script>

Used with this page.

Link to comment
Share on other sites

I don't know if className is considered as standard [w3]. It's kind of a grey area in my opinion on whether support for getAttribute('class') is the correct method of access (mozilla supports getAttribute('class') whereas IE supports getAttribute('className'), .className is universal). I can see why MSIE decided against it originally (conversions from languages like Perl), but I do think it should be fixed in the next JS verison - although I didn't see it in Brendan's proposals at his latest conference...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...