1

Can you please take a look at This Demo and let me know how I can toggle varible value from "ecolo" to "econo" using jQuery?

<button id="model">Toggle Query</button> 
<div id="result"></div>

<script>
var qtype= "econo";
$("#model").on("click", function () {
    qtype= "ecolo";
    $("#result").html(qtype);
 });
</script>

Thanks

1 Answer 1

4

Use a conditional expression in an assignment

qtype = (qtype == "econo") ? "ecolo" : "econo";

or just a simple if:

if (qtype == "econo") {
    qtype = "ecolo";
} else {
    qtype = "econo";
}

For a true/false boolean variable, you can simply write:

variable = !variable;

the ! operator performs the boolean not operation.

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

2 Comments

Thanks Barmar, Can I use same logic to toggle the true false boolean as well? by the way I still have to wait 10 minutes to accept you answer!
For a boolean you can just write variable = !variable;.

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.