0

I have an HTML table with a checkbox in each row.

I want to loop over the table and see if there are any checkboxes that are checked. The following does not work:

$(document).on("click", "#Button2", function(event)
{
        $('#mytable').find('input[type="checkbox"]:checked').each(function()
        {
                console.log($(this).text());
        });
});

https://jsfiddle.net/dHZS9/696/

In the above fiddle Initially Select some checkboxes (For example A , B , H)

On click of a button how to get names for which checkbox is selected (For Example A, B, H) in this case

2 Answers 2

1

The texts do not belong to the input elements you loop over, but to their parent elements (labels), so replace your console.log by this:

console.log($(this).parent().text());

Alternatively, you can change the selector and select the labels that have a checked checkbox as child:

$('#mytable').find('label:has(input[type="checkbox"]:checked)').each(function() {
    console.log($(this).text());
});
Sign up to request clarification or add additional context in comments.

Comments

0

The text is not within the input check, for that reason the $(this).text() does not work.

You need to go to your parent of your checkout input in this case to your label tag and now execute the .text() function to get the text.

Change your line like this:

console.log($(this).parent().text());

Regards!

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.