0

I have a table as follows. I need to give background color(yellow) only to the first header column. Also all the text color should be blue in the header (for both columns). What are the ways to achive it using jQuery?

Note: I am trying to learn jQuery. Hence the solution should be using jQuery.

   <table id = "myTable">
        <thead>
            <tr>
                <th>
                     <a href="http://www.Lijo.com">Name</a>
                </th>
                <th>
                    Address
                </th>
            </tr>
        </thead>
        <tr>
            <td>
                Lijo
            </td>
            <td>
                India
            </td>
        </tr>
    </table>

4 Answers 4

1
$("#myTable th:first").css({
    "background-color": "yellow"
});
$("#myTable th").css({
    "color": "blue"
});​

You can also achieve the same in one line:

$("#myTable th")
    .css({"color": "blue"})
    .filter(":first")
    .css({"background-color": "yellow"});​
Sign up to request clarification or add additional context in comments.

1 Comment

$("#myTable th a").css({"color": "red"}).
1

You can select the first header column like so:

var firstColumnHeader = $('#myTable thead th:first-child');

and then you can update the background colour by using the css method, like so:

firstColumnHeader.css('background', '#FCD116');

Here is a jsfiddle demostration.

Comments

1

Hiya demo http://jsfiddle.net/r3jMv/1/ (updated) (old =>) http://jsfiddle.net/Zarhu/2/

and with blue color here: http://jsfiddle.net/r3jMv/6/ another updated version from below comment: http://jsfiddle.net/cC6hk/

following should do the trick.

this might be good play ground: http://vkanakaraj.wordpress.com/2009/06/18/select-a-column-of-a-table-using-jquery/

jquery code

$(function(){
  // Change first column background color.

  $("table thead tr th:first-child").css("background-color","#ff0000");
});

$("#myTable > thead > tr >th").css("background-color","yellow");​

html

<table id = "myTable">
        <thead>
            <tr>
                <th>
                    Name
                </th>
                <th>
                    Address
                </th>
            </tr>
        </thead>
        <tr>
            <td>
                Lijo
            </td>
            <td>
                India
            </td>
        </tr>
    </table>
​

4 Comments

Yellow should be only to the first column. How do we do it?
Thanks...What do I do if I have a hyperlink to be colored as Red? jsfiddle.net/Lijo/7mCqU/1
cool cool, you want first column to be permanent colored or only when hover a mouse colored?
Cool like this: jsfiddle.net/cC6hk ? don't forget to accept the answer :))
1

This is for the first header where class name should do all the styling. The selector gets the all the "th" inside the #mytable and using the eq(0) bring the first th and add to it the class

$("#myTable th").eq(0).addClass("ClassName")

This for all the header where class name 2 should do the styling you

$("#myTable th").addClass("ClassName2")

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.