• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding onkeypress to dom with javascript

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, i am having trouble adding onkeypress event to my document using javascript. you see, i already have this function checkError (widget, x, y). this already works well with all my other forms (input) in my html file:

<tr><td>
<INPUT TYPE="TEXT" ONKEYPRES="checkError(this,5,5)" /> (this input is within a table)
</td></tr>

i wanted to add to my table dynamically so what i did was:

var x = document.getElementById('table_1').insertRow (document.getElementById('table_1').rows.length);
var img = x.insertCell (0);
var iteration = x.insertCell (1);
iteration.innerHTML = '<td>iterations: <br> <input> </input></td>';

the problem is, i want to put an onkeypress event on my input element. i'm trying to do this:

iteration.onkeypres = checkIterationError(this,5,5);

but it doesn't work. what should i do? thanks!!! i really need help on this one. thanks.
 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, here is a folow up, i changed the code a little bit and i got the function working but there's another problem:

var x = document.getElementById('table_1').insertRow (document.getElementById('table_1').rows.length);
var iteration = x.insertCell (0);
//this is the new code
iteration.innerHTML = '<td>iterations: <br></td>'
var input = document.createElement ('input');
iteration.appendChild (input);
input.onKeyUf = checkIterationError(document.getElementsByTagName('input')[0],5,5);


the problem is, checkIterationError is performed only on the first run (i.e. when a button is clicked and the input form is shown. when i say it works it means it writes in the console what i want it to write but it doesnt check if the input is character or not [this is the prime objective of the function]). but when i use the input form (whenever i type into it) the function doesn't work - both in writing what i want it to write in the console and in restricting the input to numbers only.

what should i do? thanks!!! i really need help on this one. thanks.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to do


When you do it the way you are, you are assigning the returned value of the function to the event handler. This way you are assigning a function to the event handler.

Eric
 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much! it's working now
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic