• I have created a php checkbox with a single option:

    <FORM NAME="featured">
    <input type="checkbox" name="bdchk" id="bdchk" onChange="set_check(this);window.location.reload();">
    </FORM>

    The first script creates a cookie when the box is checked and when it is unchecked the cookie is removed; it also displays a confirm message prior to checking the box where the user confirms or cancels their selection of the checkbox.

    function setCookie (cname, cvalue, exdays, path) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*1000*60*60*8));
        var expires = "expires="+d.toGMTString();
        var cname = "featured";
        var cvalue = "1";
        var path = "/";
        document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
    }
    
    function set_check(me){
      if(me.checked && confirm("Are you sure")) {
        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);
    }}

    The second script uses localStorage to keep the checkbox either checked or not checked after page refresh or navigation.

    jQuery(function ($) {
        var test = localStorage.input === 'true'? true: false;
        (jQuery)('input').prop('checked', test || false);
    });
    
    (jQuery)('input').on('change', function() {
        localStorage.input = (jQuery)(this).is(':checked');
        console.log((jQuery)(this).is(':checked'));
      }});

    The issue is that I need both scripts to use the same confirm message answer, so if the user selects “yes” to check the box then both functions will use that input. I have tried adding “confirm()” to the second script in as many ways as I could think of but it either ignores the line, or it creates a second confirm so the user has to confirm twice.

    So how can I either call the previous script’s “confirm” input in the second script, or combine these 2 scripts into one so they both use the same confirm?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I think it’ll help us understand if you provide a working example of what you’re doing.

    Thread Starter SJS719

    (@sjs719)

    Hey Andrew, I tried to create a working model on JSFiddle but since cookies and localStorage won’t work in JSFiddle the confirm box never fires and the process never runs. But here is the demo anyways: https://jsfiddle.net/sjs719/gusjbxyL/

    Basically what I am trying to create is a checkbox in the members profile page that when checked makes their profile “Featured”. So when the checkbox is checked by the user it needs to remain checked (using localStorage) and I need to be able to check if that box is checked from other pages which is why I create a cookie when the box is checked (and remove the cookie when the box is unchecked).

    But right now when the user checks the box and the confirm box appears if they select “cancel” the checkbox still gets checked instead of remaining unchecked. To fix this I believe I need to add an ‘if statement’ which checks if the confirm box has been answered “yes” or “cancel” but when I add if (confirm("Are you sure")) to the localStorage script it creates a second confirm prompt (since the confirm prompt also exists in the cookie script).

    So I need if (confirm("Are you sure")) in both scripts but only want 1 confirm prompt to fire.

    I hope this makes since

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to Combine 2 Separate Javascript Functions?’ is closed to new replies.