Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Toggle a variable value in JavaScript

Posted on 2006-05-10
Medium Priority
Last Modified: 2012-05-05

In my application, I have a OnMouseDown event. Each time the mouse is down, which trigges the event and cause a variable (global)  toggled. However, it is not working. Could someone take a look of the code below? I appreciate the help.

//a global variable
var down = -1;

function toggle(){

        if (down == -1)
            down = 1;
        if (down == 1)
            down = -1;


this fucntion toggle is called via OnMouseDown event. It seems the value is always set to -1 when mouse is down


Question by:penglu

Author Comment

ID: 16649027

Never minde. I made a mistake

Accepted Solution

radnor earned 150 total points
ID: 16649655
You cant test it like that...

How about just mult your value by -1?

1 * -1 = -1
-1 * -1 = 1
LVL 12

Expert Comment

ID: 16650109
I usually use the true/false,   down = !down;

does the same job as radnor's.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question