Analog
Analog

Reputation: 261

display hidden div attached to a option

I want to display a hidden div based on a selected radio button, for example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".option_select").click(function() {
            if ($('#extra_options').is(":hidden")) {
                $('#extra_options').slideDown("slow");
            }      
        });
    }); 
</script>        
</head>

<body>
<div id="extra_options" style="display:none;">
<input type="radio" name="ExtraOption" value="1" /> Extra option 1<br />
<input type="radio" name="ExtraOption" value="2" /> Extra option 2<br />
<input type="radio" name="ExtraOption" value="3" /> Extra option 3<br />
</div>

<form action="#" method="post">
<input type="radio" name="Option" class="option_select" value="1" /> Option 1<br />
<input type="radio" name="Option" class="option_select" value="2" /> Option 2<br />
<input type="radio" name="Option" class="option_select" value="3" /> Option 3<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

But I'd really like to attach that #extra_options div to the selected radio button ("Option 1, Option 2, Option 3 etc), so it looks more like "these extra options go with this option #". I figured I could have a #extra_optionsX div attached to every single radio button but I figured there was a better way then that. Any ideas?

TIA

Upvotes: 0

Views: 103

Answers (2)

stefanz
stefanz

Reputation: 1326

A working code is here .

$(document).ready(function() {
    $('form > input').click(function(){
        var t = $(this),
            extra = $('#extra_options');

        $('input[value=' + t.val() + ']', extra)
            .parent()
            .show()
            .siblings()
            .hide();

    });
});​

Better to wrap all inputs with text in a <label>, something like : <label><input type="radio" name="foo" value="bar" /> Its value here </label>

Upvotes: 0

VLS
VLS

Reputation: 2346

You can append it using

if ($('#extra_options').is(":visible")) {
    $('#extra_options').hide();
}
$(this).next().after($('#extra_options'));
$('#extra_options').slideDown("slow");

(edit: added hiding/showing when changing selected options)

Upvotes: 1

Related Questions