5

I am trying to to select a table row using JQuery, but it seems not to fire the .selected event. I have put the code on JSFiddle

http://jsfiddle.net/tonymaloney1971/3tevxmps/1/

I would like a table row selected when the mouse is clicked and change row colour and display an alert message with the selected row information.

I have tried the following but it doesn't work:

    $("td").click(function () {
        $('.selected').removeClass('selected');
        $(this).addClass("selected");

    });

Any ideas?

Thanks

4
  • 2
    Your code seems to work just fine.. What is the problem with it? Commented Aug 8, 2014 at 8:18
  • Like this? Commented Aug 8, 2014 at 8:19
  • You need to select the entire row, right ?? Commented Aug 8, 2014 at 8:21
  • Yes, but also display an alert box where it would should the selected row values. Commented Aug 8, 2014 at 8:47

5 Answers 5

10

try this: fiddle demo

you can add class each td like: "p" for product, "i" for inf Rate, "n" for note, and get in click event.

js changes:

$("tbody tr").click(function () {
    $('.selected').removeClass('selected');
    $(this).addClass("selected");
    var product = $('.p',this).html();
    var infRate =$('.i',this).html();
    var note =$('.n',this).html();
    alert(product +','+ infRate+','+ note);
});

css changes:

table.formatHTML5 tr.selected {
    background-color: #e92929 !important;
    color:#fff;
    vertical-align: middle;
    padding: 1.5em;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's great, but any idea how to get the selected row field values and display in an alert message box?
That is perfect, thank you very much for your help it is great learning from you guys!Please Vote this
1

You have to put the event on the table row (tr) and then change color of each table cell (td)

$("tr").click(function () {
        $('.selected').removeClass('selected');
        $(this).find("td").addClass("selected");

    });

Comments

0

You tr is inside tbody so you have to use something like this

$("#myTable tbody tr").live('click', function (event)
    {
           //adding class
           //removing class
    });

Note: live may not support in latest version of jquery . use ON accordingly

Working fiddle : http://jsfiddle.net/supercool/550nq015/

2 Comments

so why than suggesting a method (.live()) that is long-time deprecated?
roko just in case if some one using jquery 1.8.3 and below . for 1.8.3 plus i suggested to use ON in place of live
0

I checked your code.. Here is my solution.

First, the clickable element is a td element. So in JQuery function you need to ask the parent of this element. To do that you can do with this code.

$("td").click(function () {
            $('.selected').removeClass('selected');
            $(this).parents('tr').addClass('selected');

        });

It will add a class to your parent tr element from td that you click. I noticed the css you provide is only work with td element. So I write new rule for selected row element.

table.formatHTML5 tr.selected{
            background-color: #e92929 !important;
            vertical-align: middle !important;
            height: 4em;  
        }  

otherwise, you can also add onClick html event for each tr elements displayed in the table.

Hope this answer helps you

Comments

0

the Shortest way:

CSS:

<style type="text/css">

    tr.selected {
        background-color: #e92929 !important;
        color: #fff;
        vertical-align: middle;
        padding: 1.5em;
    }

</style>

Jquery:

$(".table > tbody > tr").click(function (e) {
   $(this).addClass("selected").siblings().removeClass("selected");
});

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.