Search code examples
jqueryradio-buttonjqueryform

display alert and checkbox based on radio button value


<input type="radio" id="incomplete" name="app_status" value="incomplete">Incomplete Application<br>
<input type="radio" id="complete" name="app_status" value="complete">Complete Application<br><br>
<div id="app_box">      
   <input type="checkbox" name="application_complete"  value="checked" />Checbox policy

<br><br>
</div>

I am trying to implement a radio box such that when a user clicks on the incomplete radiobox it should display an alert.

If the user clicks on the complete radio box its should show the 'application_complete' box, else hide it.

Here is what I am trying to implement in my jquery function, probably my concept is a bit wrong i guess:

<script type="text/javascript">
$(function(){
    $("#app_box").hide("fast");
    if($("#complete").is(':checked')){
        $("#app_box").show("fast");
    }
    elseif($("#incomplete").is(':checked')){
        alert("Application will be incomplete");
        $("#app_box").hide("fast");
    }
});
</script>

Thanks guys.. I need to little modification to the code suggested by JREAM. If I check on the application_complete box and then click on the incomplete radio box, can I uncheck the application_complete box again?

Never mind. i figured it out.

$('input[name=application_complete]').attr('checked',false);

Solution

  • You have two problems:

    1: You have no Event handler, so it only checks your forms when your page loads (And they have no selection made to start with)

    2: You did "elseif" but in JS its two words, so your JavaScript broke, do "else if", here is the solution:

    <script type="text/javascript">
    $(function(){
        $("#app_box").hide();
    
        $('input[name=app_status]').change(function() {
          var value = $(this).val();
          if (value == 'incomplete') {
            alert('error msg');
          } else if (value =='complete') {
            $('#app_box').show();
        }
    });
    
    });
    </script>