1

i have this check box with different name but all the name start with checkUser

<input type="checkbox" value="60176694447" name="checkUser52106">
<input type="checkbox" value="60176694448" name="checkUser52107">
<input type="checkbox" value="60176694449" name="checkUser52108">

A quick google bring me this function from sitepoint

$(function(){
$('#btnClick').click(function(){
var val = [];
$(':checkbox:checked').each(function(i){
  val[i] = $(this).val();
});
});
});

Right now i want to get all the value of selected checkbox with name start with checkUser. How to achieve this?

2 Answers 2

2

Use the attribute startsWith selector:

$(':checkbox[name^="checkUser"]:checked').each(function(i){
    val[i] = $(this).val();
});

You can use .map instead for a shorter solution:

var val = $(':checkbox[name^="checkUser"]:checked').map(function(){
    return this.value;
}).get();
Sign up to request clarification or add additional context in comments.

Comments

0

Use a CSS3 type selector like this

$('input[name=~"checkUser"]:checked').each( function(idx) {
    val[idx] = $(this).val();
});

Edit: oh, same exact answer as above :P

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.