Javascript, "if" statement won't work?
-
Here is my "if" statement: <script type="text/javascript"> <!-- var unlockproperty = 0; if (unlockproperty > 1) {document.write ("hello");} //--> </script> now here is my function that adds 1 to my "unlockproperty" variable (it is called by simple button) <script type="text/javascript"> <!-- function addigit() { unlockproperty=unlockproperty+1 } //--></script> My problem is, whenever "unlockproperty" becomes greater than 1, because of the button calling the "addigit" function, nothing happens.....its supposed to write "hello".....help me out......
-
Answer:
You are on the right track. You are increasing unlock property when the function is called, but your if statement (and document.write) must ALSO be in the function if you want it to execute with the function. You can either do that in a separate function like the other answer or include it into your original function like this: function addAndWrite() { unlockproperty=unlockproperty+1; if(unlockproperty > 1){document.write("hello!");} }
GoshFath... at Yahoo! Answers Visit the source
Other answers
At what point does the "if" statement ever get called again? Ever? I look at your code, and I see the "if" statement is in the global area, not inside a function, so it will be executed exactly once when the page loads. And when the page loads, there's a nice little assignment that sets it to 0 before testing it. So, in a nutshell, that "if" statement can never evaluate to "true". That "hello" will never be executed. What you probably want is something more like the following: function hello_if_unlocked(unlocked) { if (unlocked >1) {document.write("hello");} } function addigit() { unlockproperty = unlockproperty + 1; hello_if_unlocked(unlockproperty); }
The if statement is fine. There's nothing wrong with it. The problem you're running into concerns when the browser even checks the if statement at all. Right now, it checks it once, as soon as the code is loaded, and then forgets about it. When the function addigit() is called, it increments unlockproperty as expected, but the browser doesn't go back and check that if statement because it was never told to. There are a number of ways to get around this, depending on what precise behavior you want. One simple way would be to put the if statement inside its own function, and then call that function from inside addigit() after incrementing unlockproperty. On the other hand, you could try implementing something made of event handlers or timers.
Related Q & A:
- HP printer won't work?Best solution by Yahoo! Answers
- Cruise control won't work?Best solution by Yahoo! Answers
- My webcam won't work on MSN.?Best solution by Yahoo! Answers
- Why my PSP START button won't work?Best solution by answers.yahoo.com
- Yahoo plug-ins won't work.Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.