The Reason
The Reason

Reputation: 7973

How to get the last value from multiple checkbox?

This my checkbox elements:

<input type="checkbox" name="colors" value="blue" /> Blue <br/>
<input type="checkbox" name="colors" value="green" /> Green<br/>
<input type="checkbox" name="colors" value="yellow" /> Yellow<br/>

How can i get the last value from this multiple checkbox, just clear JS because i have been studying JS and HTML just for 2 days. Thanks.

Upvotes: 0

Views: 140

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Use document.querySelectorAll

var checkboxes = document.querySelectorAll("input[name=colors]")
var lastCheckbox = checkboxes[checkboxes.length - 1];
var lastCheckboxValue = lastCheckbox.value;

Upvotes: 1

Related Questions