tags:

views:

24

answers:

2

I am planning a following html output with PHP. Now when I select week, I want to slide in week div, selecting day,day div etc.

How can I do it with jquery.

<div id="main">
 <form>
<div id="week_day_time">

<input type="radio" name="week_day_time" value="week" />Week<br />
<input type="radio" name="week_day_time" value="day" />Day<br />
<input type="radio" name="week_day_time" value="time" />Time

</div>
<div id="week">
more form for week.
originally hidden by css. But it will open when you select a week radio button.
more form for week
</div>
<div id="day">
more form...
originally hidden by css. But it will open when you select a day radio button.
more form for day
</div>
<div id="time">
more form...
originally hidden by css. But it will open when you select a time radio button.
more form for time
</div>
</form> 
</div>
+2  A: 

Try this:

var current = '';
$('[name="week_day_time"]').change(function() {
    if(current.length) 
        $('#' + current).slideUp();
    current = $(this).val();
    $('#' + $(this).val()).slideDown();
});

Working Example

Jacob Relkin
I tried it here. http://jsfiddle.net/Fj5mb/1/, but I am doing something wrong.
shin
@shiri, See my updated answer.
Jacob Relkin
super! Thanks Jacob.
shin
@shin, You're welcome. You can upvote as well you know :)
Jacob Relkin
I voted, Jacob. Why do you need var i=0?
shin
@shin, Oops, good catch. That was for the other solution that I was thinking of.
Jacob Relkin
+1  A: 

I would do more or less the following: http://jsfiddle.net/dactivo/dhwZY/

I would wrap the texts in a div called "texts", and exec the following function:

$('input[name="week_day_time"]').click(function() {

    $("#texts").children().slideUp();
    $('#' + $(this).val()).slideDown();

});
netadictos
the method "change" will also work.
netadictos
Once you wrap all the texts in one div, you can also slideUp everyone, like this: $('#' + $(this).val()).siblings().slideUp(); $('#' + $(this).val()).slideDown(); With this, there is no need for using any variables.
netadictos