简体   繁体   中英

alert text field value when someone enters

I have a text field say

<input id="hello" type="text" name="suraj" />

When someone enters a value in this field, Is there a way to alert that value (without any button click) in javascript or jquery.

Please help. Thanks

On keypress you will get multiple alerts. If you handle on focusout then you can see the typed value. Use this code if you can handle on focusout.

$('#hell').on('focusout', function(){
  alert(event.target.value);
})

You can use .keyup() event as shown below :-

$('#hell').on('keyup',function(){
   alert($(this).val());
});

Fiddle

Or(if you want to alert text as soon as user finished typing then use .blur() event as shown)

$('#hell').on('blur',function(){
  alert($(this).val());   
});

Fiddle

Or you can also use .focusout() event as shown

$('#hell').on('focusout',function(){
    alert($(this).val()); 
});

Fiddle

here are some following event for keyboard

http://api.jquery.com/keypress/

http://api.jquery.com/keydown/

http://api.jquery.com/keyup/

and try

$('#hell').on('keyup',function() {
       alert($(this).val());
    });

or

$('#hell').on('keydown',function() {
       alert($(this).val());
    });

or

$('#hell').on('keypress',function() {
   alert($(this).val());
});

Just use focusout event, this way..

$('#hell').on('focusout',function(){
   alert($(this).val());
});

Or you can use blur

$('#hell').on('blur',function(){
  alert($(this).val())      
});

You can use keyup event like below

$('#hell').on('keyup',function()
{
   alert($(this).val());
});

Please Try this.

 <input id="hell" type="text" name="suraj" onChange="toggle()"/>

    <script language="JavaScript">
        function toggle() {
       var value = document.getElementById('hell').value;
       alert(value);
        }
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM