0

i have an html table that is displayed on my web application, but i would like to select a row by clicking on it and then have a button to get each columns data as variables i can use in javascript. this is what i was trying but it's not complete because i do not know what to do. remember i want what's in the columns. my function...

function selectedRows() 
{
   var selectedItems = ('#ScannedLabelTabl; // i wanted to get the selected item (row)

     $.each(selectedItems, function (i, item) {


      });

}
2
  • How do you handle the click on the table? A click handler for every table cell? If so, you can find the index of the clicked cell in its row. Commented Feb 22, 2013 at 9:30
  • Is the table dynamic or static? If static, it may be that you can create the data as an object in JavaScript and then use that to write the table but also an easier reference for the event handler... Commented Feb 22, 2013 at 9:35

5 Answers 5

1
$('tr').click(function() {
    $('tr').removeClass('selected');
    $(this).addClass('selected');

    var td = $(this).children('td');
    for (var i = 0; i < td.length; ++i) {
        alert(i + ': ' + td[i].innerText);
    }
});

Run demo here: http://jsfiddle.net/VjkML/

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

Comments

0

The easiest way I could think of doing this would be to add a class to the row/column when the user clicks the element. CSS styles could apply, and jQuery's class selector could be used to enumerate the selected elements.

I'll build a JSFiddle.

Aww. Too slow. Props to Ovilia.

Comments

0

assuming you want to allow multiple selections (not sure if that's what you want) you may add a click event handler to the cells that do

$("tr").click(eventHandler);

function eventHandler(){
    //what this does is verifies of the element has the class, if it doesn't have the class is addeds, if it has the class it's removed
    $(this).toggleClass("selected");
}

then to get all the values do something like this:

function getAllValues(){
    var arrayOfValues = [];
    $(".selected").each(function(){
        var value = $(this).//what you want to get val or text
        arrayOfValues.push()

    });
    return arrayOfValues;
}

Comments

0

Try below code

  $(function () {

    $("td").click(function () {
        $("td", $(this).parent()).each(function () {
            alert($(this).html());
        })
    })

Comments

0
var tableRows = $("#ScannedLabelTabl").find("tr");//fetching all rows of the tblebody
$.each(tableRows, function( index, value ) {//iterating all fetched rows
     var tdValue=$(value).find("td:nth-child(3)").html();   
     (value).find("td:nth-child(3)").addClass("adding");//get particular column

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.