0

Problem

I'm trying to use two functions as conditions within an if else statement.

Then, upon either of them returning true, they carry out a desired process.

However, I can only get them to work independently, not as part of the if else statement.

What I've got

Currently, they work fine independently:

let input = document.querySelector('searchBox');

searchBox.addEventListener('keyup', (e) => {
    if(e.keyCode === 13) {
  alert(e.target.value);
  return true;
  }
})

$("#searchBtn").click(function(){
alert("Button clicked");
//alert("Button clicked");
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="search-bar" class="input-group custom-search">
                        <input id="searchBox" type="text" value="" class="form-control custom-search-input" placeholder="Search for product/material" >
                        <button id="searchBtn" class="btn btn-primary custom-search-botton" type="submit" >Search</button>  
                    </div>

What's not working However, I'm trying to get them running as part of an if else statement. This is what I've got, but it's not turning over...

function enterPressed() {
  $(document).on('keypress', function(e) { //enter button
    if (e.which == 13) {
      alert('You pressed enter!');
      return true;
    }
  })
}

function buttonClicked() {
  //a button (equivalent to buttonClicked)
  $("#searchBtn").click(function() {
    alert('You clicked the button!')
    return true;
  })
}

function start() {
  if (enterPressed == true || buttonClicked == true) {
    alert('You entered some information!!')
  }
}

enterPressed();
buttonClicked();
start();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="search-bar" class="input-group custom-search">
  <input id="searchBox" type="text" value="" class="form-control custom-search-input" placeholder="Search for product/material">
  <button id="searchBtn" class="btn btn-primary custom-search-botton" type="submit">Search</button>
</div>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
e-driver1
  • 45
  • 7
  • I fixed a couple of typos in the final snippet, hope that's OK. What precisely is the issue - is it that you were expecting to see `alert('You entered some information!!')`? – CertainPerformance Dec 24 '22 at 16:59
  • Why don’t you call the functions? `buttonClick` itself is never equal to `true` – Nico Haase Dec 24 '22 at 17:00
  • It sounds like you're just trying to return the result from the asynchronous input handlers to your `start` function. You will need to wait for there to be an input before checking the result, in order to have any results (and you'll also need to use either a couple of outer variables or a Promise to compare - comparing the functions themselves `enterPressed == true` doesn't make sense) – CertainPerformance Dec 24 '22 at 17:03
  • Thanks for the pointers, both. Will take them on board and get back on the issue on here. FYI I am trying to see alert('You entered some information!!'). – e-driver1 Dec 24 '22 at 17:14

0 Answers0