Question

In the following Fiddle, you can click to select rows in the table. If you click the 'Execute' button, an alert will tell you if the class .row_selected is visible or not. This is all working, now I need to elaborate on the rows selected part. The user can only 'Execute' one row at a time, so if one row is selected - yay. If more than one are selected, an error message asking to select only one row. One row to rule them all. Any ideas?

http://jsfiddle.net/BWCBX/34/

jQuery

$("button").click(function () {
    if ($(".row_selected").is(":visible")) {
        alert('Row(s) are selected.')
    } else {
        alert('No rows are selected.')
    }
});
Was it helpful?

Solution

Add a condition with .length see below,

if ($(".row_selected").length > 1) {  //more than one row selected
    alert('Please select one row');
} else if ($(".row_selected").length) { //one row selected
    alert('Row(s) are selected.')
} else {  // none selected
    alert('No rows are selected.')
}

Seems like the row_selected is applied to the row only on selection so you don't need :visible check.

DEMO: http://jsfiddle.net/7wrJC/

OTHER TIPS

You can use the following code to get the number of the selected rows:

if (1 === $(".row_selected:visible").length) {
    // do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top