1

I'm trying to figure out a code where if one or more checkbox is checked through a form submit, they would show their content. I'm close to doing it but the code only works in single instead of multiple checks. The content also gets stuck even though it's been unchecked after it's checked.

$(document).ready(function(){   
    $(function(){
        $('form').submit(function(e){
            e.preventDefault();

            if ($('.checkEdu').is(':checked')) 
            {   $('#education').show();
            } else if ($('.checkWork').is(':checked')){
                $('#experience').show();
            } else if ($('.checkAbout').is(':checked')){
                $('#about').show();
            }
            else {
                $('#education').hide();
                $('#experience').hide();
                $('#about').hide();
            }
        });
    });
});

This is the code I tried. Thanks

4
  • It would be nice to include the minimal HTML needed to reproduce your problem. Commented Apr 19, 2018 at 20:18
  • also could you elaborate on "the code only works in single instead of multiple checks" ? Commented Apr 19, 2018 at 20:20
  • Start by hiding all of them, then in seperate if's unhide accordingly. Since you are if-else you only get 1 or the other. Commented Apr 19, 2018 at 20:20
  • $(document).ready(function() {} and $(function() {} do the same thing. Commented Apr 19, 2018 at 20:21

3 Answers 3

2

I would suggest hiding them all first and then showing the ones that are selected:

$('#education').hide();
$('#experience').hide();
$('#about').hide();
if ($('.checkEdu').is(':checked')){
    $('#education').show();
}
if ($('.checkWork').is(':checked')){
    $('#experience').show();
}
if ($('.checkAbout').is(':checked')){
    $('#about').show();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, I actually just thought about this code and check up on this to remove it. haha!
1

Try this. Since more than one can be checked at a time they need separate if checks.

$(function(){
    $('form').submit(function(e){
        e.preventDefault();
        $('#education').hide();
        $('#experience').hide();
        $('#about').hide();            
        if ($('.checkEdu').is(':checked')){   
            $('#education').show();
        }
        if ($('.checkWork').is(':checked')){
            $('#experience').show();
        }
        if ($('.checkAbout').is(':checked')){
            $('#about').show();
        }

    });
});

Comments

0

Your HTML code will be like this,

<form>
<input type="checkbox" class="checkEdu">
<input type="checkbox" class="checkWork">
<input type="checkbox" class="checkAbout">
<button type="submit">Submit</button>
</form>

<div id="education" style="display:none;">education</div>
<div id="experience" style="display:none;">experience</div>
<div id="about" style="display:none;">about</div>

add following jquery code

$(function(){   
    $('form').submit(function(e){
        e.preventDefault();
        $('#education, #experience, #about').hide();
        $('[type="checkbox"]:checked').each(function(k,v){
            switch($(v).attr('class')){
                case 'checkEdu' : $('#education').show(); break;
                case 'checkWork' : $('#experience').show(); break;
                case 'checkAbout' : $('#about').show(); break;
            }
        });
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.