March 14, 2012

Button to select all check boxes and change entire table's bg color

Question by Unicorn

this is what i have so far:
– a table with 20 to 30 cells
– each cell has 1 check box and a value (different value of course)
– two button to trigger select all and select none checkboxes.

Codes so far:
– to trigger select all/none:

Javascript:

function set_checked(checked) {
    $('input[name=test]').attr('checked', checked);
}

buttons:

onclick="set_checked(true)" and onclick="set_checked(false)" for both buttons.

now, how do i add color change on both buttons? for example, entire table bg color change to green when select all and change to white when select none.

Answer by Starx

This should do, what is needed

function set_checked(checked) {
    $('input:checked').attr('checked', checked); //select all checkbox
    if(checked) {
         $("table").css("backgroundColor", "#ddd"); //new color
    } else {
         $("table").css("backgroundColor", "#fff"); //old color
    }
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!