0

I am working on a scenario - let's say

var clicked=false;
var operate=false;

I want something like:

if((myButon is clicked) || (operate == true))
{
//escalate operation
}

I could use $('myButton').click(function(){ }); to get the click operation but how can I combine this with the OR condition as expressed above?

5 Answers 5

1

Try this:

$('#myButton').click(function(){
    if(operate){
        // do smth
    }
});

Or:

$('#myButton').click(function(){
    if(!operate) return false;
    // do smth
});
Sign up to request clarification or add additional context in comments.

1 Comment

Your answers make sense to me and the way I want to write the code is in line with Max and Liam but for whatever reason, it isn't working. But if I use the ('#myButton').click(function(){}); without any added condition it works. I am still troubleshooting it... Aditzu I get your point, but I need to combine the conditions somehow. Thanks guys.
1
$('myButton').click(function(){ 
  if(operate){
   // do something
  }else{
    // otherwise... let it go or do some other stuff
  }
});

3 Comments

wrong... you applied here and operand not or. 'Do something' must be runnable also without click on button when 'operate' is true
@Aditzu that condition can either be executed on page load or on some event...that will not execute auto-magically..
of course that condition can be executed on whatever event he want. In your case event is trigged only if user clicks button and operate is true (which is if((myButon is clicked) && (operate == true)) not if((myButon is clicked) || (operate == true)) )
0

Try something like this:

var clicked = false;
var operate = false;

$("#myButton").click(function() { clicked = true });

if((clicked) || (operate))
{
//escalate operation
}

This should work just as you'd like to, the only thing that could happen is that the script runs before the button has been clicked, so you could check it more often by doing something like this:

var escalatedRun = false;

function check() {
    if((clicked) || (operate)) {
        //escalate

        escalatedRun = true;
    }
}

do {
   window.setInterval(check(), 1000);
} while (!escalatedRun)

Comments

0
$('myButton').click(function(){
    if(operate){
         // do smth
    }
});

or

$('myButton').click(function(){ 
  if(operate == true){
   // do something
  }else{
    // otherwise... let it go
  }
});

are wrong because they applyed 'and' operand not 'or' as @bdfios wish.

The correct answer is :

$( document ).ready(function() {
 $('myButton').click(function(){ 

       // do something

    });

if (operate == true){
       // do something
}
});

Comments

-1

If I understand you correctly, are you looking for this?

$('myButton').click(function(){
   if((myButon === clicked) || (operate === true))
   {
   //escalate operation
   }
});

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.