user700284 user700284 - 24 days ago 8
jQuery Question

Styles not getting applied properly in dynamically created radio buttons

I have to create a radio button group dynamically.I am able to successfully create it.But the problem is that the dynamically created radio buttons do not look like the static ones.

I tried two approaches:
1) Called

create
on the
fieldset
-
ui-corner-all
class is getting added on an inner span and as a result all radio buttons appear rounded.
2) Called
create
on the div that encloses
fieldset
- Here the problem is better.But still some problems exist.At first,when there is only radio button,
ui-controlgroup-last
class is getting added to that radiobutton.Next,when the second radio button is added,the
ui-controlgroup-last
class is getting added to that radiobutton.But the
ui-controlgroup-last
is still present in the first radio button and this cycle repeats.As a result all radio buttons have the styles of last radiobutton.

This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<script>
$(document).ready(function()
{
for (var i = 0; i < 3; i++)
{
$("#rgroup1").append('<input type="radio" name="rg1" id="radio1-'+i+'"/><label for="radio1-'+i+'">Value</label>');
$("#rgroup1").trigger("create");

$("#rgroup2").append('<input type="radio" name="rg2" id="radio2-'+i+'"/><label for="radio2-'+i+'">Value</label>');
$("#radiodiv1").trigger("create");

}
});

</script>
</head>

<body>
<div data-role="content">
<h3>Normal radio buttons</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup">
<input type="radio" name="rg" id="radio-1"/><label for="radio-1">Value</label>
<input type="radio" name="rg" id="radio-2"/><label for="radio-2">Value</label>
<input type="radio" name="rg" id="radio-3"/><label for="radio-3">Value</label>
</fieldset>
</div>
<h3>Dynamically created-Div Refresh</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup1">
</fieldset>
</div>
<h3>Dynamically created-Fieldset Refresh</h3>
<div id="radiodiv1">
<fieldset data-role="controlgroup" id="rgroup2">
</fieldset>
</div>
</div>
</body>




P.S - The reason why I am calling create after each append is because thats how it works in my actual usecase-Only one radio button is created at any time.

The generated source

<html lang="en" class="ui-mobile"><head><base href="http://localhost:8081/html5/calendar-new/radio_issue.html">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css">
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<script>
$(document).ready(function()
{
for (var i = 0; i < 3; i++)
{
$("#rgroup1").append('<input type="radio" name="rg1" id="radio1-'+i+'"/><label for="radio1-'+i+'">Value</label>');
$("#rgroup1").trigger("create");

$("#rgroup2").append('<input type="radio" name="rg2" id="radio2-'+i+'"/><label for="radio2-'+i+'">Value</label>');
$("#radiodiv1").trigger("create");

}
});

</script>
<title></title></head>

<body class="ui-mobile-viewport"><div data-role="page" data-url="/html5/calendar-new/radio_issue.html" tabindex="0" class="ui-page ui-body-c ui-page-active" style="min-height: 933px; ">
<div data-role="content" class="ui-content" role="main">
<h3>Normal radio buttons</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup" class="ui-corner-all ui-controlgroup ui-controlgroup-vertical">
<div class="ui-radio"><input type="radio" name="rg" id="radio-1"><label for="radio-1" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-top ui-btn-up-c"><span class="ui-btn-inner ui-corner-top"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div>
<div class="ui-radio"><input type="radio" name="rg" id="radio-2"><label for="radio-2" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-btn-up-c"><span class="ui-btn-inner"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div>
<div class="ui-radio"><input type="radio" name="rg" id="radio-3"><label for="radio-3" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div>
</fieldset>
</div>
<h3>Dynamically created-Div Refresh</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup1" class="ui-corner-all ui-controlgroup ui-controlgroup-vertical">
<div class="ui-radio"><input type="radio" name="rg1" id="radio1-0"><label for="radio1-0" data-theme="c" class="ui-btn ui-btn-icon-left ui-btn-corner-all ui-radio-off ui-btn-up-c"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg1" id="radio1-1"><label for="radio1-1" data-theme="c" class="ui-btn ui-btn-icon-left ui-btn-corner-all ui-radio-off ui-btn-up-c"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg1" id="radio1-2"><label for="radio1-2" data-theme="c" class="ui-btn ui-btn-icon-left ui-btn-corner-all ui-radio-off ui-btn-up-c"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div></fieldset>
</div>
<h3>Dynamically created-Fieldset Refresh</h3>
<div id="radiodiv1">
<fieldset data-role="controlgroup" id="rgroup2" class="ui-corner-all ui-controlgroup ui-controlgroup-vertical">
<div class="ui-radio"><input type="radio" name="rg2" id="radio2-0"><label for="radio2-0" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-top ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-top ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg2" id="radio2-1"><label for="radio2-1" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg2" id="radio2-2"><label for="radio2-2" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div></fieldset>
</div>
</div>

</div><div class="ui-loader ui-body-a ui-corner-all" style="top: 290px; "><span class="ui-icon ui-icon-loading spin"></span><h1>loading</h1></div></body></html>

Answer

I fixed this issue by explicitly removing the ui-controlgroup-last and ui-corner-bottom classes from all the labels except the label for the last radio button

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
    <script>
$(document).ready(function()
{
    for (var i = 0; i < 3; i++)
    {
        /*$("#rgroup1").append('<input type="radio" name="rg1" id="radio1-'+i+'"/><label id="label1-'+i+'" for="radio1-'+i+'">Value</label>');
        $("#rgroup1").trigger("create");*/


        $("#rgroup2").append('<input type="radio" name="rg2" id="radio2-'+i+'"/><label id="label2-'+i+'"for="radio2-'+i+'">Value</label>');
        $("#radiodiv1").trigger("create");


    }
    for (var i = 0; i < 2; i++)
    {
        $('#label2-'+i).removeClass("ui-controlgroup-last ui-corner-bottom");
    }
});

    </script>
</head>

<body>
  <div data-role="content">
    <h3>Normal radio buttons</h3>
    <div id="radiodiv">
        <fieldset data-role="controlgroup" id="rgroup">
            <input type="radio" name="rg" id="radio-1"/><label for="radio-1">Value</label>
            <input type="radio" name="rg" id="radio-2"/><label for="radio-2">Value</label>
            <input type="radio" name="rg" id="radio-3"/><label for="radio-3">Value</label>
        </fieldset>
    </div>
    <!-- <h3>Dynamically created-Div Refresh</h3>
    <div id="radiodiv">
        <fieldset data-role="controlgroup" id="rgroup1">
        </fieldset>
    </div> -->
     <h3>Dynamically created-Fieldset Refresh</h3>
    <div id="radiodiv1">
        <fieldset data-role="controlgroup" id="rgroup2">
        </fieldset>
    </div>
  </div>
</body>

Comments