How to show alert when enter key pressed

Ahmed Haiba

I'm trying to get the alert message but it doesn't show at all. It only shows if I deleted (event) from : function myFunction(event).

Can you help me fix it so it can show the alert box when I type less than three letters? Here's the code that I'm stuck with :

function myFunction(input, e) {
  var input, filter, table, tr, td, i;
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  if (filter.length < 3 && e.key === "Enter") {
    alert(
      "Search is going to work only if the phrase contains at least 3 characters."
    );
    return;
  }
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[1];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<h2>My Customers</h2>

<input type="text" id="myInput" onkeyup="myFunction(this, event)" placeholder="Search for names.." title="Type in a name">

<table id="myTable">
  <tr class="header">
    <th>Name</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris specialites</td>
    <td>France</td>
  </tr>
</table>


</body>
</html>

How can I solve it?

edited: I added the html code to show more about the problem.

Alex Kudryashev

See working example.

function myFunction(input, e) {
  var filter, table, tr, td, i;
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (var i = 0, t; t = tr[i]; ++i) {
    if (t.className !== 'header')
      t.style.display = 'none'
  }
  //start search only after Enter pressed
  if (e.key !== 'Enter')
    return;
  //and length >=3
  if (filter.length < 3) {
    alert("Search is going to work only if the phrase contains at least 3 characters.");
    return;
  }
  //continue with search here
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[1];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      }
    }
  }

}
<input type="text" id="myInput" onkeyup="myFunction(this, event)" placeholder="Example: Mauro, etc." />
<table id="myTable">
  <tr class="header">
    <th>Name</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris specialites</td>
    <td>France</td>
  </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

jQuery: content to show when enter key is pressed

How to show a div when the enter key has been pressed in two textareas?

i want alert some thing every time when i pressed enter key

Every time when i pressed the enter key then it should alert some thing

How to do something when Enter-key is pressed on NSTextField

How can I fire the Tab keypress when the Enter key is pressed?

How to make a function do something when the enter key is pressed

How to change focus to another component when Enter key is pressed

How can I know when the Enter key was pressed on QTextEdit

How to move to next input field when enter key is pressed?

How to have a function called when Enter key is pressed in a guizero application?

How to disable the <KeyRelease> event only when the enter key is pressed

how to make the code not break when the enter key is pressed?

Submit a form only when 'enter' key is pressed

Invoke a serie of actions when Enter key is pressed

Form not submitting when enter key is pressed

Call a javascript function when enter key is pressed

How to trigger code if Enter key is pressed in a column

How to write "enter key pressed" to a stream?

How to check if user pressed Enter key ?

how to check if the enter key is pressed python

Reaction Time Game - How would I show some message/error for when enter is pressed before stated 'GO!!'?

Enter key pressed fast

how get a row index from gridview windows form when enter key pressed

How to get input textfield values when enter key is pressed in react js?

how to avoid changing of ng bootstrap dropdown when enter key is pressed on any on input field in angular13

How to prevent cursor from moving to next textbox when Enter key is pressed?

How to Detect When the Enter Key was Pressed using F sharp (F#)

How to submit and retrieve text from dynamic textbox when Enter key is pressed in Windows forms using C#?