0

I have problem with JavaScript/jQuery. I'm trying to change variable's value after dialog is displayed and if checbox is checked, but this don't work when I check value of it. What can I do instead or this code is wrong?

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>JS/jQuery</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
  <script type="text/javascript" src="myjs.js"></script>
</head>
<body>
  <div class="back"></div>
  <div class="start">   
    <label for="chex">Is this checkbox checked?</label>
    <input type="checkbox" id="checkexist" name="chex" />
    <button id="run">Run form</button>
  </div>
  <div class="window"></div>
</body>
</html>

JavaScript:

$(document).ready(function() {

  var start = false;
  var check = false;

  if (start == false) {
    $("#checkexist").click(function(){
        if (this.checked) {
            check = true;
        }
    });
    $("#run").click(function(){
        start = true;
        $(".start").fadeOut('slow', function(){
            $(".start").css("display", "none");
        });
        $(".back").fadeOut('slow', function(){
            $(".back").css({'display' : 'none', 'opacity' : '0', 'filter' : 'alpha(opacity=0)'});
        });
    });
  } else {
    if (check == true) {
        $(".window").click(function(){
            $(".window").hide('fast');
        });
    } else {
        //do something
    }
  }
});
4
  • "this don't work when I check value of it" What does it do instead of what you expect? And also, you will get more help if yo recreate this example on jsfiddle.net and post the link to it. Commented Sep 21, 2012 at 18:17
  • 1
    Your else statement will never run because it's inside the document ready function which runs only when the page is loading. Commented Sep 21, 2012 at 18:17
  • it is really not clear what you want to do. Do you want to hide the .window div when the checkbox is checked? I think it will be easier if you explain what you try to do. It seems there is no value that you try to change... what exactly are you talking about? Commented Sep 21, 2012 at 18:25
  • Div classes "start" and "back" are like lightbox. When you click on Run form, this "lightbox" form closes down and the value of checkbox #checkexist should be in variable "check". If it's true then when you click on .window (some form) closes itself. But if it's not true, then do something else (I will work on that after I solve this problem). If the document is already loaded, what can I do instead of changing variable if the checkbox is checked? Commented Sep 21, 2012 at 20:45

1 Answer 1

1

ok, so lets try solving this bit by bit.

your code runs inside $(document).ready(function() {}); and as you were told by others, if (start == false) {} this part will never run: else{}, since when the page loads, start will be always set to false (var start = false;) next,

$("#checkexist").click(function(){
        if (this.checked) {
            check = true;
        }
    });

could be replace with: $('#checkexist').is('checked') which will return true or false based on the checkmark. true for checked, false for unchecked.

now, assuming we know that, and we know you want to run this when the button is clicked:

$('#run').on('click', function(){
    // close the start and back
    $(".start,.back").fadeOut('slow');
    if ($('#checkexist').is(':checked'))
    {
        // checkbox is checked, close the .window
        $(".window").hide('fast');
    }else{
        //checkbox was not checked, what to do?
    }
});

Had a small syntax error. it work fine now. You can see it work here : http://jsfiddle.net/fp7kV/8/

sorry, missed the :

Sign up to request clarification or add additional context in comments.

3 Comments

This also doesn't work. Checkbox is always unchecked. link
@user1257255 - please check again, it had a small syntax error which i fixed.
Your fiddle doesn't work - when I check checkbox and click Run, the .window doesn't hide itself. I'm using Chrome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.