Nubtacular
Nubtacular

Reputation: 1397

Rails issue with using Jquery to select only one checkbox from multiple

I am aware I should be using radio buttons from a UX standpoint but I was told to use checkboxes. Here is the gist to the HAML: https://gist.github.com/2701998.

As you can see there is a checkbox for 'Check', a checkbox for 'Credit Card', and then 4 Credit Card options. Ideally the user can only select either Check OR Credit Card, and if they select Credit Card, they can only select one of the four Credit Card types (either Visa, Amex, Discover, or Mastercard).

This is my current code, it's not doing much of anything. Any help is greatly appreciated.

var checkboxes = $(':checkbox.payment-split);

checkboxes.click(function(){

  var self = this;

  checkboxes.each(function(){

    if(this!=self) this.checked = ''

  })

})

Upvotes: 0

Views: 1738

Answers (1)

Mark Paine
Mark Paine

Reputation: 1894

var $check_boxes = $('input[type=checkbox]');
$check_boxes.click(function() {
  $check_boxes.prop('checked', false);
  $(this).prop('checked', true);
}

Upvotes: 3

Related Questions