simontabor / jquery-toggles

jQuery plugin to make easy toggle buttons
http://simontabor.com/labs/toggles
MIT License
362 stars 67 forks source link

Multiple items using JQuery Toggle select type #56

Closed justinlee34 closed 9 years ago

justinlee34 commented 9 years ago

I am using toggle select type and got issue of setting the status value if using for multiple items. I tried to set an unique div id for each toggle class. Each item has a description and status. How do I go about setting the value of the status for each item?

This is my jquery statement e.g. :

$("#itemsdiv div").each(function(index){ var $this = $(this);
console.log($(this).attr("id")); var item = $(this).attr("id"); $("#"+item).on('toggle', function (e, active) { if (active) { console.log("active"); $('#requestForm').find("input[name='"+item+"Status']").val("Y"); } else { console.log("non active"); $('#requestForm').find("input[name='"+item+"Status']").val("N"); } });

Any advice will be good. Thanks.

Cheers, Justin

simontabor commented 9 years ago

You can give toggles a checkbox to control by giving it one in the options, so you don't need to do the logic yourself.

$("#itemsdiv div").each(function(index){
  var item = $(this).attr("id");
  $('#' + item).toggles({
    checkbox: $('#requestForm input[name="'+item+'Status"]')
  });
});

This simply updates the checked property of the checkbox with the toggle (https://github.com/simontabor/jquery-toggles/blob/master/js/Toggles.js#L257).

justinlee34 commented 9 years ago

Noted thanks a lot :)