0

I am rendering a list of checkboxes with values and want to retrieve these values as I am checking the correlating checkboxes.

{{each Items}}
       <tr>

           <td><input type='checkbox' name='chkRelatedTopics' value='${subject}'/></td>
           <td><label id='labRelatedTopicDisplay'>${subject}</label>
       <tr>

{{/each}}

as I am doing it at the moment I am getting the first value of the first checkbox rendered every time.

$(document).on("click", "input[name=chkRelatedTopics]", function () {

   var nameAdminUser = $("input[name=chkRelatedTopics]").val();
   alert(nameAdminUser);
});

How can I get the value of the subsequent checkbox I am checking?

2 Answers 2

3

change this

 $("input[name=chkRelatedTopics]").val();

to

 $(this).val();
Sign up to request clarification or add additional context in comments.

Comments

2

you need to get the value of the clicked checkbox for that you can use $(this).val()/this.value inside the click handler as this points to the clicked dom element

if you use $("input[name=chkRelatedTopics]").val() it will return the value of the first element matching the said selector causing it to return the same value regardless of which checkbox was selected

$(document).on("click", "input[name=chkRelatedTopics]", function () {
    var nameAdminUser = $(this).val();
    alert(nameAdminUser);
});

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.