2

I try to change the background of rows in my table with JavaScript but it's not working. This is my code. However I tried to create a table with html and its work. P.S : I want to use webkit.

Code :

var table = document.getElementById("tab");
var rowCount = table.rows.length;


for(var i=0;i<6;i++) {     
   row = table.insertRow(rowCount);
   row.id = "row"+i;
   cell1 = row.insertCell(0);
   var content = document.createElement("output");                
   content.innerHTML = i ;
   cell1.appendChild(content);
   rowCount++;
}


 $(document).ready(function(){
        $('td').each(function(i) {     
             $('#tab').on('click', '#row'+i, function(){
                document.getElementById("row"+i).style.background = 'white';
                 //alert(i);
             });   
        });   

     //example2
     $('#td1').on('click', function(){
                document.getElementById("td1").style.background = 'white';
             });   
 }); 
1

5 Answers 5

2

The issue you're running into is because you are setting the background gradient on the TD, then setting the background color of the TR. Since the TD is within the TR, you're just not seeing your change be applied.

Sign up to request clarification or add additional context in comments.

4 Comments

How can I add the TR with javaScript ?
you could change the JS, or you could change the CSS. See this update to your fiddle for how to get what you want with updated CSS: jsfiddle.net/e2rYy/18
@SlimMils Appreciate the accepted answer as updating the CSS does fix your problem, but you should accept and use Mike McCaughan's answer. It will perform better and keep your solution cleaner
@nybler: The solution that works more with my problem it's yours. I saw the answer of Mike and I thank him for his effort.
2

You shouldn't assign event handlers in a loop, especially when you don't need to:

$('#tab').on('click', 'tr', function () {
    $('td,th', this).css('background', 'white');
});         

fiddle.

Comments

1

It's backgroundColor, not background.

2 Comments

@SlimMils: HTML, CSS, and the JavaScript DOM are different technologies. So, they can have different names for the same features.
Just as @JohnFisher said. JavaScript uses camelCase (for example backgroundColor, instead of background-color in css).
1

First issue is the Closure Issue.. ..

The Click event will always point to the last instance of i .

Change your code to this

$('td').each(function(i) {
     (function(num) {
        $('#tab').on('click', '#row' + num, function() {
           document.getElementById("row" + num).style.background = '';
          //alert(i);
        });
     })(i)
 });

This might not be the main reason for the non functional code..

Comments

0

Do you want to change the color of the complete row or the current cell? Your current selector would only apply to a specific cell.

// for the complete row
$(document).ready(function(){
    $('td').on('click',function(){
        $(this).closest('tr').find('td').css({'background':'red'});
    });
}); 

// or for the active cell..
$(document).ready(function(){
    $('td').on('click',function(){
        $(this).css({'background':'red'});
    });
}); 

See this fiddle http://jsfiddle.net/axelmichel/2KUCz/ for the row solution. In case you want a different behaviour for each table, you could modify the selector in:

$('#tab').find.('td').on('click',...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.