How to Combine 2 Separate Javascript Functions?
-
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?
- The topic ‘How to Combine 2 Separate Javascript Functions?’ is closed to new replies.