views:

20

answers:

1

jQuery UI seems to be sensitive to the attributes of the elements that compose a widget. How then can I use the same widget mutiple times in a single page\form?

Example - the radio select widget, which comes in the demo index.html file that downloads with jQuery-ui:

<!DOCTYPE html> 
<html>
    <head> 
        ...
        $(function(){
            $("#radioset").buttonset();
        }
        ...
    </head>
    <body>
        ...
        <form style="margin-top: 1em;"> 
            <div id="radioset"> 
                <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
                <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label> 
                <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
            </div> 
        </form> 
        ...
    </body>
</html>

What code then should I add to have a second div with a different set of radio buttons?

+1  A: 
   $(function(){
            $("#radioset2").buttonset();
        }
<div id="radioset2">

Or you could use

$(".radioset").buttonset();

Wich would select the radioset class not the id so you could have many.

To address your comment:

$(function(){
                $("#radioset1").buttonset();
                $("#radioset2").buttonset();
            }

<form name="form1" style="margin-top: 1em;"> 
            <div id="radioset1"> 
                <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
                <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label> 
                <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
            </div> 
</form>
<form name="form2" style="margin-top: 1em;"> 
            <div id="radioset2"> 
                    <input type="radio" id="radioAgain1" name="radio" /><label for="radio1">Choice 1</label>
                    <input type="radio" id="radioAgain2" name="radio" checked="checked" /><label for="radio2">Choice 2</label> 
                    <input type="radio" id="radioAgain3" name="radio" /><label for="radio3">Choice 3</label>
                </div> 
        </form> 

I see no reason for the above not to work.

Iznogood
Pretty sure jquery needs them to be something1 something2. Try it out. You CAN have many radiosets on a page its trivial.
Iznogood
You're right but with one critical addition - the two radio sets need to be in seperate forms, or else all the radio buttons are tied together. Actually I don't see the logic in that, but I tested it and that was the differentiating element. @lznogood - do the change and I'l accept your answer.
Jonathan
@Jonathan Done!
Iznogood