• I am using the following javascript function to prompt the user to confirm their selection when they check a checkbox:

    function set_check(me){
    checkedMsg = confirm("Are you sure");
      if(me.checked && checkedMsg) {
        setCookie(me.value, me.checked, 1);
        console.log(me.value);
        console.log(me.checked);
        console.log(document.cookie);
    } else if (! me.checked) {
        setCookie(me.value, me.checked, -1);
        console.log(me.value);
        console.log(me.checked);
        console.log(document.cookie);
    }}  
    
    <FORM NAME="featured">
    
    Featured:
    <input type="checkbox" name="feat" id="feat" onChange="set_check(this)">
    
    </FORM>

    I declared the confirm prompt as “checkedMsg” because I need to use the same prompt again to validate a separate script. But this script currently displays the confirm box when a user is unchecking the box as well as when they check the box and I need the confirm box to only appear when the box is being checked.

    When I don’t declare the confirm prompt as “checkedMsg” and instead just use if(me.checked && confirm("Are you sure") { then it will only show when the box is checked like I want, but then I cannot call this same confirm prompt in my other scripts.

    So how can use the confirm prompt in the IF statement and stop the confirm from carrying over to the ELSE statement? Any help is greatly appreciated!

  • The topic ‘Display Confirm Prompt Only When Checkbox is Checked (Not when Unchecked)’ is closed to new replies.