Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  165] [ 5]  / answers: 1 / hits: 16396  / 10 Years ago, mon, october 20, 2014, 12:00:00

I am trying to trigger a function when value of input_1 textbox changes.



i.e. getElementById('input_1').onchange results in object is not a function.



HTML



<form method=GET id=game_form></form>
<table border=1 style=background-color:#FFFFCC;border-collapse:collapse;border:1px solid #FFCC00;color:#000000;width:100 cellpadding=3 cellspacing=3>
<tr>
<td id=cell_1><input type=text id=input_1 form=game_form value=x></td>
<td id=cell_2><input type=text id=input_2 form=game_form></td>
<td id=cell_3><input type=text id=input_3 form=game_form></td>
</tr>
<tr>
<td id=cell_4><input type=text id=input_4 form=game_form></td>
<td id=cell_5><input type=text id=input_5 form=game_form></td>
<td id=cell_6><input type=text id=input_6 form=game_form></td>
</tr>
<tr>
<td id=cell_7><input type=text id=input_7 form=game_form></td>
<td id=cell_8><input type=text id=input_8 form=game_form></td>
<td id=cell_9><input type=text id=input_9 form=game_form></td>
</tr>
</table>


Javascript



$(window).load(
function checkInput() {
// alert(I am an alert box!1);
var cell = document.getElementById('input_1');
alert(cell.value);
**document.getElementById('input_1').onchange**(
doSomething()
);
}
);


function doSomething(){
alert(I am an alert box!2);

}

More From » jquery

 Answers
24

You're using the wrong syntax. You want addEventListener



var el = document.getElementById(input_1);
el.addEventListener(change, doSomething, false);


Or with jQuery:



var $el = $('#input_1');
$el.on('change', doSomething);

[#69076] Thursday, October 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dakotahs

Total Points: 605
Total Questions: 104
Total Answers: 113

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;