1

I'm trying to make a boolean change in this value by using a function. I tried it this way:

var itemInStock = false;

$("#button").click(function(){
    itemInStock = true
}
if( itemInStock === false){
    document.write("item is out of stock");
} else{
    document.write("item is in stock");
}

I understand why it's not working but I can't find a way to solve this!

4
  • You cannot use document.write after load and is also not necessary also the Boolean changes when the button is clicked and not before Commented Jan 15, 2013 at 20:18
  • 2
    That will always write item is out of stock. Commented Jan 15, 2013 at 20:20
  • You likely want to toggle the status, not set it blindly. Maybe. It's not really clear what you're trying to do. Commented Jan 15, 2013 at 20:22
  • Don't add "SOLVED" to the title. Accept an answer instead. Commented Jan 16, 2013 at 17:51

4 Answers 4

3

I just can guess what you're trying to achieve. It seems like you wanna check at some point, if item is currently in stock. Since you can't know when the click will occur, one solution could be periodically checking the value.

(function () {
    var itemInStock = false;

    $("#button").click(function () {
        itemInStock = true
    });

    window.setInterval(function () {

        if (itemInStock === false) {
            console.log("item is out of stock");
        } else {
            console.log("item is in stock");
        }

    }, 500);
})()

http://jsfiddle.net/Ttu5N/

Tell me, if I'm wrong with my guessing.

Update: way easier approach

$(function () {
    var $state = $('#state');


  $state.text('item is out of stock');

    $("#button").click(function () {
        $state.text('item is in stock');
    });


})

<button id="button">click me</button>
<div id="state"></div>

http://jsfiddle.net/Wb3ET/ Just do it directly on click.

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

1 Comment

Great! Thanks! This is exactly what I was searching for !
0

because itemInStock doesn't get changed until the button is clicked...

2 Comments

But how can I solve this so it does say "item is in stock"(for example) when I click the button?
you need to put that alert in a button handler.
0

You cannot use document.write after load and is also not necessary also the Boolean changes when the button is clicked and not before

Create a span with id="status" and have

var itemInStock = false;
$(function() {  
  $("#button").click(function(){
    itemInStock = true;

    $("#status").html("item is in stock":
  }

  $("#status").html(itemInStock?"item is in stock":"item is out ofstock");
});

Comments

0
// HTML
<div id="status">item is out of stock by default</div>

// JS
var itemInStock = false;

$("#button").click(function(){
    itemInStock = true;
}, changeStatus());

function changeStatus(){
    if( itemInStock === false){
         $('#status').html("item is out of stock");
    } else{
         $('#status').html("item is in stock");
    }
}

Comments

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.