views:

59

answers:

2

Need to apply a var to a statement if its conditions are met, this syntax isn't throwing errors but its not working.

<script type="text/javascript">
$(document).ready(function(){
  var action_is_post = false;
      //stuff here
    $(this).ready(function () {
        if ($("#stepDesc0").is(".current")) {
        action_is_post = true;     
        }
    });
  //stuff here
</script>

should I use something other than .ready? Do I even need the $(this).ready(function ()... part? I need it to apply the var when #stepDesc0 has the class current.

$(document).ready(function(){ var action_is_post = false; $("form").submit(function () { action_is_post = true; }); if ($("#stepDesc0").is(".current")) { var action_is_post = true; } window.onbeforeunload = confirmExit; function confirmExit() { if (!action_is_post) 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.'; } });
+2  A: 
<script type="text/javascript">
  $(document).ready(function(){
    var action_is_post=$("#stepDesc0").is(".current");
  });
</script>

If you want the variable to be accessible outside the $(document).ready(function(){..., then you'll need to declare it outside the statement like this:

<script type="text/javascript">
  var action_is_post;

  $(document).ready(function(){
    action_is_post=$("#stepDesc0").is(".current");
  });
</script>

HTML (in order to test it):

<a href="javascript:alert(action_is_post);">Show value</a>
Gert G
Should have posted all the code. It pertains to a form wizard, I have already disabled the onbeforeunload for the submit function, now I need to disable it for the first step of the wizard.
Dirty Bird Design
<script type="text/javascript">$(document).ready(function(){ var action_is_post = false; $("form").submit(function () { action_is_post = true; }); $(this).ready(function () { if ($("#stepDesc0").is(".current")) { action_is_post = true; } }); window.onbeforeunload = confirmExit; function confirmExit() { if (!action_is_post) 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.'; }});</script>
Dirty Bird Design
Sorry, I can't seem to format code on this
Dirty Bird Design
A: 
$(function() {
    var actionIsPost = $('#stepDesc0').is('.current'); 
    alert( actionIsPost );
});

If you are adding the current class to #stepDesc0 on an event then put the .is check in the event handler.

meder