Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html <input type="text" /> onchange event not working

I am trying to do some experiment. What I want to happen is that everytime the user types in something in the textbox, it will be displayed in a dialog box. I used the onchange event property to make it happen but it doesn't work. I still need to press the submit button to make it work. I read about AJAX and I am thinking to learn about this. Do I still need AJAX to make it work or is simple JavaScript enough? Please help.

index.php

<script type="text/javascript" src="javascript.js"> </script>  <form action="index.php" method="get">  Integer 1: <input type="text" id="num1" name="num1" onchange="checkInput('num1');" /> <br />  Integer 2: <input type="text" id="num2" name="num2" onchange="checkInput('num2');" /> <br />  <input type="submit" value="Compute" /> </form> 

javascript.js

function checkInput(textbox) {  var textInput = document.getElementById(textbox).value;   alert(textInput);  } 
like image 344
Newbie Coder Avatar asked Jun 09 '11 09:06

Newbie Coder


People also ask

How do I use Onchange input field?

The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.

How do you trigger an event when a textbox is filled with value?

change function , but that needs to be done only if the the value is changed using the button . $("#address"). change will trigger if you perform any change in text field. If you want to trigger alert only when button click why you don't use alert inside button click function.

What is Onchange in input?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.


2 Answers

onchange is only triggered when the control is blurred. Try onkeypress instead.

like image 85
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 12:09

Ignacio Vazquez-Abrams


Use .on('input'... to monitor every change to an input (paste, keyup, etc) from jQuery 1.7 and above.

For static and dynamic inputs:

$(document).on('input', '.my-class', function(){     alert('Input changed'); }); 

For static inputs only:

$('.my-class').on('input', function(){     alert('Input changed'); }); 

JSFiddle with static/dynamic example: https://jsfiddle.net/op0zqrgy/7/

like image 31
rybo111 Avatar answered Sep 30 '22 11:09

rybo111