Skip to content
Advertisement

JQuery if two conditions are met output message

I’m trying to output success to the log if two conditions are met in an if statement in jQuery.
My click functions are working fine and changing the values. I just can’t get the if statement to work:

<script>
        

        var netflixClicked = 0;
        var primeClicked = 0;
        var searchClicked = 0

        $("#netflix").click(function() {
            netflixClicked = "1";
            console.log('netflix' + ' ' + netflixClicked)
        });
        $("#prime").click(function() {
            primeClicked = "1";
            console.log('prime' + ' ' + primeClicked)
        });
        $("#searchButton").click(function() {
            searchClicked = "1";
            console.log('search' + ' ' + searchClicked)
        });
        $(document).ready(function() {
            if ((netflixClicked == 1) && (searchClicked == 1)) {
                console.log('success')
            }
        });
    </script>

Advertisement

Answer

that ?

const BtClick =
  { netflix : 0
  , prime   : 0
  , search  : 0
  } 
$("#netflix, #prime, #search").click(function()
  {
  BtClick[this.id]++
  console.log(this.id , BtClick[this.id] )

  if (BtClick.netflix && BtClick.search) {
    console.log('success')
    }
  })
.as-console-wrapper { max-height:100% !important; top:0; left:70% !important; width:30%; }
.as-console-row         { background-color: yellow; }
.as-console-row::after  { display:none !important; }
.as-console-row::before { content: 'log:'; font-size: .8em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="netflix" > netflix </button>
<button id="prime"   > prime   </button>
<button id="search"  > search  </button>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement