Search code examples
phpjqueryradio-buttonshow-hidereset

Clear all field inputs in div on hide() jquery: radio button with php function call


I'm quite new to jQuery, so I'm having trouble with this.

I have divs that show or hide according to which radio button is selected, but I cannot figure out how to make all of the fields reset if a div is hidden.

The divs have date input fields with php drop down menus that are set to Null or "" in the first option. There are also some additional radio buttons within the main toggled divs (with more date choices/menus) - So a div within a div with more radio buttons...

Ultimately, I want to clear/reset everything within any hidden divs.

Here is my Jquery (I was getting desperate towards the end and started looking for straight up javascript after a while - don't know what I'm doing there either).

The .desc class is the main radio button choice that shows or hides one of two divs with various other choices.

    $(document).ready(function(){ 
    $("input[name='date']").change(function() {
        var test = $(this).val();
        $(".desc").hide();
        $("#"+test).show();
        $("input[type='hidden']").remove();
    }); 

    $("input[name='year']").change(function() {
        var test = $(this).val();
        $(".rnge").hide();
        $("#"+test).show();
        $("input[type='hidden']").remove();
    }); 
});

Here is the html (sorry for not adding it sooner.

<div id="formElementLeft">
    <h4>Date:</h4>
<p>Is this an exact or an approximate date?</p> 
    <p><input type="radio" name="date" value="exact"/>Exact</p>
    <p><input type="radio" name="date" value="circa"/>Approximate</p>   
</div>       
    <!--if exact show this-->
<div id="exact" class="desc" style="display:none;">
<p>Enter the exact date: <? echo date_picker(); ?><input type="radio" name="bce_ce"   value="BCE"/>BCE  <input type="radio" name="bce_ce" value="CE"/>CE</p>        
</div>
    <!--if approximate show this-->
<div id="circa" class="desc" style="display:none;">
    <p>Enter a date range.</p>             
    <p>is this a single year or a range of years?
    <p><input type="radio" name="year" value="single"/>Single year</p>
    <p><input type="radio" name="year" value="range"/>Range of years</p>        
    <div id="single" class="rnge" style="display:none;">
    <p><? echo year_picker(); ?></p>
    <p><? echo month_picker(); ?> (optional)</p></div>
    <div id="range" class="rnge" style="display:none;">
    <p>Between <? echo year_picker(); ?><input type="radio" name="bce_ce" value="BCE"/>BCE     <input type="radio" name="bce_ce" value="CE"/>CE;</p> 
    <p>and <? echo year_picker(); ?><input type="radio" name="bce_ce" value="BCE"/>BCE  <input type="radio" name="bce_ce" value="CE"/>CE</p></div>               
</div>

Thanks in advance for helping poor, confused me.


Solution

  • Thank you for the tips. It took walking away from it for a day and looking at it with fresh eyes to really understand the syntax. Eventually my tinkering led to this, which actually works(quite proud of myself)!! I nested the .html method in the .hide method so the .html method would be dependent on the .hide method. The trouble with the .each method was that it prevented the divs from showing at all when a radio button was selected.

    $(document).ready(function(){ 
    $("input[name='date']").change(function() {
        var test = $(this).val();
        $(".desc").hide(function(){
            $(this).html($(this).html());
            });     
        $("#"+test).show();
    }); 
    
    $("input[name='year']").change(function() {
        var test = $(this).val();
        $(".rnge").hide(function(){
            $(this).html($(this).html());
            }); 
        $("#"+test).show();     
    }); 
    

    } });

    EDIT: Discovered a glitch. using the .desc class was not specific enough for one of the divs so it was clearing some things and not allowing one div to open. This is better and does exactly what I want. I had to nest the .html method in the first .show function for this and specify a div id instead of a class to make it work.

    $(document).ready(function(){ 
        $("input[name='date']").change(function() {
            var test = $(this).val();
            $(".desc").hide();      
            $("#"+test).show(function(){
                $("#exact").html($("#exact").html());
                });
        }); 
    
        $("input[name='year']").change(function() {
            var test = $(this).val();
            $(".rnge").hide(function(){
                $(this).html($(this).html());
                }); 
            $("#"+test).show();     
        }); 
    });