views:

46

answers:

1

Hi, I have a form broken into 5 steps. Currently if you refresh/exit/close etc on steps 2-4 it returns a message via window.onbeforeunload no issues there.

window.onbeforeunload = function () {
            if ( $("#step1").is(":visible") || $("#step2").is(":visible") || $("#step3").is(":visible") ) {
                return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
            }
        }

Now I need to add the same or similar type of behavior on step0 (first step) if checkbox "A" is checked and checkbox "B" isn't. This works, but not combined with another .onbeforeunload function

window.onbeforeunload = function () {
            if ( $("#A").is(":checked") && $("#B").is(":not(:checked)") ) {
                return 'Message here. Do you wish to continue?';
            }
        }

How can I chain these two together? using an if, else statement doesn't seem to work.

+2  A: 

You can combine them in one function like this:

window.onbeforeunload = function () {
  if ( $("#A").is(":checked") && !$("#B").is(":checked") ) {
      return 'Message here. Do you wish to continue?';
  }
  if ( $("#step1, #step2, #step3").filter(":visible").length) {
      return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
  }
}

I simplified your other selector to grab all elements, .filter() only :visible ones and see if there are any (.length > 0), this is just an easier-to-maintain way of going about it.

Also you may want to add an additional check to the first if, ensure you're on that step, like this:

if ($('#step0').is(':visible') && $("#A").is(":checked") && !$("#B").is(":checked") ) {
Nick Craver
Hey Nick, here is page https://www.kinetick.com/purchaseTest it's not working as desired. Maybe I don't need the onbeforeunload for #step0? It doesn't get triggered when I hit next, only when I refresh. Should this be a validation thing? If so how do I avoid getting in a loop if the choice is desired? It's not an absolute that "#B" is checked if "#A" is, just a warning.
Dirty Bird Design
@Dirty - In your case it sounds like you want the A/B check on the first `next` button, is that correct?
Nick Craver
yes, for some reason this isn't working...<script type="text/javascript"> $("#step0Next").one('click', function() { if($("#A").is(":checked") } }); </script>
Dirty Bird Design
@Dirty - Probably because those elements are created on the fly, make sure that code runs after your formToWizard code does, or make it `live` like this: `$("#step0Next").live('click', function() { if($("#A").is(":checked") $(this).die('click'); } });`
Nick Craver
@Nick - That kind of worked, it didn't stop the form from going onto step 2, which would be desired. How do I get it to prevent the step from moving on? Also, why doesn't ".one" work, using ".live" didn't seem like the right choice, which now I see it is. what does $(this).die('.click') do?
Dirty Bird Design
@Dirty - It emulates the `.one()` behavior, removing the handler after the first run, if you want to prevent doing, instead of `alert("foo")`, after the `.die()` do `return "Are you sure you want to whatever?"`, this should prevent going to step 2 if they click cancel.
Nick Craver
@Nick - man. I understand the concept, but adding return made nothing work. Also, how do I make it fire if replacing "#B" with a group consisting of "#B, #C, #D etc" meaning if none of these are checked do the function? creating a var with all of them didn't seem to do it
Dirty Bird Design
$("#step0Next").live('click', function() { var groupfoo = ("#A, #B, #C, #D, #E, #F, #G") if($("#RT").is(":checked") $(this).die('click'); } });
Dirty Bird Design
@Dirty - To see if any are checked, do `if($("#A, #B, #C, #D, #E, #F, #G").is(':checked')) { alert("None are checked!"); }`, `.is()` returns true if *any* of them match the selector, so `is(':not(:checked))` returns true if *any* are not checked, which doesn't seem like the validation you want.
Nick Craver
@Nick - Here is exactly what I need to happen. upon the user clicking #step0Next (the next step button) check to see if "#A" is checked. If "#A" is checked and if none of "#B, #C, #D, #E, #F, #G" are checked return (like .onbeforeunload dialogue box) (with the continue "Yes"(allows you to go on) or "No"(closes dialogue box, but stay on that page) on step0, not on step1. Sorry for being unclear previously.
Dirty Bird Design
@Nick - when I chain #A, #B, #C, #D, #E, #F, #G like above, it would require all of them have to be checked won't it?
Dirty Bird Design
Nick Craver
@Nick, sorry didn't see the "!" in your example. how do I get the return to work, I can only get it to fire on the next step with an alert box.
Dirty Bird Design
@Dirty - Does a `return false` prevent it correctly?
Nick Craver
@Nick - No, the group thing works fine. I need to get the return dialogue box to pop on the first step now.
Dirty Bird Design
@Dirty - Sorry a bit busy with work here, I would ask a new question specifically on that, elaborating on that code in your question would help, and I'd include where the current event handlers for `#step0Next` are hooked up.
Nick Craver
@Nick - no worries. I appreciate all the help you've given. I just can't get preventdefault(); to work as expected. I have asked the question here http://stackoverflow.com/questions/3399606/help-with-preventdefault
Dirty Bird Design
@Dirty - I think it might be working as expected, just *after* the tab switch handler has run, so you need to manually switch back to tab 1 maybe?
Nick Craver
@Nick - not sure how I would do that. I dont get why preventdefault(); won't work, isn't that the point of it? preventing default behavior?
Dirty Bird Design
@Dirty - It has no default behavior though, a default behavior example would be clicking a submit button and it submitting, or clicking and anchor and it going to the `href`, this is a jQuery click handler on `#step0Next` at work, not any default behavior :)
Nick Craver
@Nick - thx for the explanation. Maybe ill try adding it to the formToWizard.js file.
Dirty Bird Design
No luck. still stuck.
Dirty Bird Design
@Nick - https://www.kinetick.com/Test/purchaseTestusing a var i was able to set a flag, but it still doesn't prevent it from moving on.$("#step0Next").live('click', function(event) { var ask = confirm("Do You Really Want To Continue"); if($("#RT").is(":checked") } });
Dirty Bird Design