skribe skribe - 4 months ago 22
Javascript Question

Jquery input get value of self and sibling inputs on event

I am trying to get the value of an input (radio button) when it is clicked on and the value of its siblings. Using Jquery I can get the value of its siblings with the code below, but can't think of how to include the value of the selected element itself.

options = $(button).siblings('input:checkbox:checked').map(function () {
return this.value;
}).get();

Answer

You can traverse to parent element and then find checked elements in it this will also ensure that it is not considering current checkbox when it is unchecked:

$(button).parent().find('input:checkbox:checked').map(function () {
  return this.value;
}).get();