I have a problem where by I want to remember the checked radio button in a jQuery UI dialog popup.
I encountered some strange behaviour so I mocked it up in JSFiddle. The fiddle basically randomly selects one of 3 radio choices when its created. However it soon beings to stop working.
I've tried with and without the line that does removeAttr('checked') as well.
You will need to click open dialog and close it several times to see it stop working. I have been testing this in the latest version of Chrome.
HTML
<div id="dialog" style="display:none;">
<input type="radio" class="left" name="rentFrequency" value="1" />
<span class="left"> Monthly   </span>
<input type="radio" class="left" name="rentFrequency" value="2" />
<span class="left"> Quarterly   </span>
<input type="radio" class="left" name="rentFrequency" value="3" />
<span class="left"> Annualy   </span>
</div>
<input type="button" name="dialogOpen" value="Open Dialog" />
<input type="text" name="randVal" value="" readonly="readonly" />
Javascript
$(document).ready(function() {
$('input[name="dialogOpen"]').click(function(){
$("#dialog").dialog({
title: 'Additional Tenancy Information',
resizable: false,
width: 530,
show: { effect:'fade', duration:500 },
hide: { effect:'fade', duration:400},
modal: true,
close: function (event, ui) {
},
open: function (event, ui) {
var randval=Math.floor(Math.random()*3)+1
console.log(randval);
$('input[name="randVal"]').val(randval);
$('input[name="rentFrequency"]').removeAttr('checked');
$('input[name="rentFrequency"][value="'+randval+'"]').attr('checked', 'checked');
}
});
});
});
This is effectively the same code just without the dialog popup and it behaves as expected:
Just keeping clicking on RUN