amitshree
amitshree

Reputation: 2298

check if all elements with similar IDs are selected using jQuery

I want to check if all buttons starting with id approve- are selected. Buttons are dynamically created and number of buttons are not constant.

<button type="button" id="approve-<?php echo $item->getId();?>">Approve</button>

<button type="button" id="reject-<?php echo $item->getId();?>">Reject</button>

I know about a function $j("button[id^='approve-']" but following code alerts "All Selected" even if one of the button is clicked.

if($j("button[id^='approve-']")){
        alert("All Clicked");
      }

Upvotes: 0

Views: 47

Answers (1)

Balachandran
Balachandran

Reputation: 9637

You can add the attribute for which button is clicked ,then check approve button length with clicked approve button length

$(document).on('click', 'button', function () {
    $(this).attr('clicked', true);
    if ($("button[id^='approve-']").length == $("button[id^='approve-'][clicked]").length) {
        alert("all approved clicked")
    }
});

DEMO

Upvotes: 2

Related Questions