0

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?

10
  • What do you mean by "get the data"? Commented Aug 19, 2014 at 1:00
  • you should post your code(markup/javasciprt) Commented Aug 19, 2014 at 1:00
  • 3
    What does it mean to “select” a row? Do you have checkboxes? Commented Aug 19, 2014 at 1:00
  • stackoverflow.com/questions/5142422/… Commented Aug 19, 2014 at 1:01
  • @AndersonGreen—that link isn't helpful. Commented Aug 19, 2014 at 1:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Eclecticist, I already figured out what you have suggested in the post above and did exactly what you have written in the code.

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.