Carlos Corral Carvajal
Carlos Corral Carvajal

Reputation: 526

jquery mobile trigger click doesn't work the first time

I have a jquery mobile form with several radio inputs whit the options "DS", "NO" and "YES". I want when I click a button it simulates a click on the "YES" option of all radio buttons.

I use this code:

$("#btn-all-yes").click(function(){
    $(".replay-yes").each(function(index, element){
        $(element).trigger("click");                  
        //$(element).click();       
    });
});

But I need to do click two times in the button to achieve the desired result. If I put the line '$(element).trigger("click")' two times it works, but I think it should work with a single call to the click event.

You can see all the code at http://jsfiddle.net/qwLPH/7/

Upvotes: 3

Views: 2987

Answers (2)

Omar
Omar

Reputation: 31732

You need to change its' status using .prop and then refresh it .checkboxradio('refresh').

Working demo

Update - Uncheck other buttons

$("#btn-all-yes").click(function () {
 $('[data-role="controlgroup"]').find("[type=radio]").each(function () {
  if ($(this).hasClass('replay-yes')) {  
   $(this).prop('checked', true).checkboxradio("refresh");
  } else {
     $(this).prop('checked', false).checkboxradio("refresh");
    }
 });
});

Old answer

$("#btn-all-yes").click(function(){
 $(".replay-yes").each(function(index, element){
    $(element).prop('checked', true).checkboxradio("refresh");                  
 });
});

Upvotes: 4

Jai
Jai

Reputation: 74738

Try to initialize the click on page load:

$(".replay-yes").each(function (index, element) {
    $(element).trigger("click");
    //$(element).click();       
}).click();
//-^^^^^^^----------this way

Tryout in fiddle here

Upvotes: 4

Related Questions