tags:

views:

85

answers:

2
$("#table").click(function(e) {
                var row = jQuery(e.target || e.srcElement).parent();
                $('#table tr').bind('click', show);
                name= row.attr("id");
            });

I am not getting the id value very first time i click on the row? second time I am getting fine? can anyone tell me why its happening like this?

A: 

Well, for one thing you might not be firing the event on a TD, but it could be anything inside that TD, such as a SPAN. That would make the parent() something other than the row you expect it to be. Try using $(e.target).closest("TR").

Also, in JQuery you don't need to use e.srcElement, it will normalize the event object for you.

EDIT: Your question still isn't very clear to me, but based on your comments you want to pop up the row id whenever you click on a row:

$("#table").click(function(e) {
            var row = $(e.target).closest("TR");
            name=$(row).attr("id");
            show(row);
        });

However this is not consistent with your comment below where you say you want the second click to show the second row, third click to show the third row, etc.

Plynx
yes i did but I am not able to get row value?for second click I need to display the second row value and third click I need to dispaly thired row value. thanks
kumar
take a look at `.closest('tr')` instead of `.parents('tr')`
gnarf
@gnarf Hey, thanks!
Plynx
+2  A: 

It looks like you're binding an event to a row when you click on the table. This will mean that the onclick handler you're binding to the row won't be bound (and therefore, won't be executed) until after you've clicked on the table at least once.

But maybe I don't fully understand what you're trying to do. Could you explain what your ultimate goal here is?

Syntactic
Yes what I need to do.. do i need to place td insetead of tr?or can you sujjest me for first click it should work.. tahnks
kumar
Oh, good catch. Syntactic is right--your `.bind('click', show)` only happens after the table is clicked in your function. If you want it to show up at once, you want to use the snippet in my answer and immediately use your show function on that.
Plynx
what is wrong with that? thanks
kumar
kumar, what exactly do you want to have happen here? What do you want to happen to the row when you click on it? Do you want to call `show` on it?
Syntactic
thanks Syntactic,the things should happend here is when you click on the row..you need to popup that row id..but here its not heppning for my first time click on the row.. second time its working fyn?thanks
kumar