I have a html table and a button below it, I want to get the data from the selected row whenever that button is clicked. How shall I go about it?
-
What do you mean by "get the data"?PM 77-1– PM 77-12014-08-19 01:00:49 +00:00Commented Aug 19, 2014 at 1:00
-
you should post your code(markup/javasciprt)Yaje– Yaje2014-08-19 01:00:54 +00:00Commented Aug 19, 2014 at 1:00
-
3What does it mean to “select” a row? Do you have checkboxes?Ry-– Ry- ♦2014-08-19 01:00:56 +00:00Commented Aug 19, 2014 at 1:00
-
stackoverflow.com/questions/5142422/…Anderson Green– Anderson Green2014-08-19 01:01:01 +00:00Commented Aug 19, 2014 at 1:01
-
@AndersonGreen—that link isn't helpful.RobG– RobG2014-08-19 01:02:12 +00:00Commented Aug 19, 2014 at 1:02
1 Answer
If I understand what you mean, you want a user to be able to select a row out of a table by clicking on it. Then when they click the button, you can capture the selected row's data in each cell.
I'll use JQuery in the explanation.
You will want to create an event handler for a click event on a <tr>. When that <tr> is clicked, you can apply a CSS class such as .selected to it. Then when the user clicks the button, you have another event handler loop through the .selected class's children and return the values (which would be <td>'s).
$('tr').click(function() {
$('.selected').removeClass('selected');
$(this).addClass('selected');
});
$('#submit').click(function() {
$('.selected').children().each(function() {
alert($(this).html());
});
});
Here is a working example on JSFiddle using JQuery.