Dirty Bird Design
Dirty Bird Design

Reputation: 5553

applying a var to an if statement jquery

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.'; } });

Upvotes: 1

Views: 142

Answers (2)

Gert Grenander
Gert Grenander

Reputation: 17104

<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>

Upvotes: 2

meder omuraliev
meder omuraliev

Reputation: 186762

$(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.

Upvotes: 0

Related Questions