tags:

views:

22

answers:

3

I want to have a form with an input text field and be able to respond (display an alert text box) if the user types in the word 'hello' (case insensitive match).

What event do I need to trap (the text input field does not seem to have a change event.

This is what I have so far:

<html>
   <head>
      <title>Some test</title>
      <script type="text/javascript" src="jquery.js"></script>
   </head>
   <body>
      <form action="something.php" method="post">
         Field1: <input id="field1" type="text">
      </form>
<script type="text/javascript">
$(document).ready(function(){
  // what?
});
</script>   
   </body>
</html>
+1  A: 

Try:

$(document).ready(function(){
  $('#Field1').keypress(function(){
    if ($(this).val() === 'hello'){
      alert('hello entered !!');
    }
  });
}); 
Sarfraz
A: 

It does

<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<link href="style.css" type="text/css" rel="stylesheet"> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    $(function(){
        $('#input1').change(function(){
            if ($(this).val() == 'hello'){
                alert('you typed hello !');
            }
        });
    });
</script>
<style type="text/css"></style>
</head>
<body>
    <input id="input1" type="text" />
</body>
</html>
Dan
A: 

I would use the keyup event coz it suits better to what you're intending to do. same codes already posted, just different event.

Thiago Santos