cancel
Showing results for 
Search instead for 
Did you mean: 

On keydown form input alert message

On keydown form input alert message

Hi all, 

I have a contact form that I´d like to create an alert message on text input keydown and I can´t make it work. 

I´d tried to follow some solutions as below but nothing happens. 

$('#mobile').on('input',function(e){

     if($(this).data("lastval")!= $(this).val()){

     $(this).data("lastval",$(this).val());

        //change action

         alert(‘my message.'); 

     };

});

I´d also tried solutions as https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeydown_onkeyup but it causes other js to stop working on site when I use it like that onkeydown="keydownFunction()" in my input directly. 

 

can anyone help? 


Thanks in advance Smiley Happy 

3 REPLIES 3

Re: On keydown form input alert message

Hello @anna carolina_caro ,

 

Trust you are doing well.

 

Checkout this code and try again.

 

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
	$('#mobile').on('input',function(e){
      if($(this).data("lastval")!= $(this).val()){
		  $(this).data("lastval",$(this).val());
  		  alert('my message.'); 
     } 
    	
   });
});
</script>
</head>
<body>

<input type="text" id="mobile" >


</body>
</html>

Hope this helps you,

Problem solved? Hit the Kudos button and "Accept it as Solution"

Re: On keydown form input alert message

@zoyascootg16c3  Thanks for replying! 

I´d tested and the alert showed but it crashed some functions on my site. 

It seems that what makes it crash was the googleapi script 

because if I delete it functions start working again but of course the alert stoped showing. 

Any other idea on how to create this alert without the googleapi script? 

Re: On keydown form input alert message

Guys, anyone has a different solution?