473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Assign onkeypress-function to dynamically created input-boxes

Hi.

I have a problem with assigning an onkeypress-function to dynamically
created input-boxes.I want to put the content of an input-field into a
tag-list when the user hits enter. This works fine the first time (when
the input-field is created in a non-dynamical way). The next
input-field is created dynamically by a function that is called when
the user hits enter (the previously generated input-field will be
hidden). Then I'm trying to assign an onkeypress-function to the new
element like this:

document.getElementById('tags_element_' +
document.getElementById('tagcount').value).onkeypr ess = function(event)

But when i press a key in the dynamically created input-field, nothing
happens. Why is this?

Some sample-code below:
----------------------------------------------------------------------------------------------------------------------------------
document.getElementById('tags_element_' +
document.getElementById('tagcount').value).onkeypr ess =
function(event){
modus = 1;
e = event;

// New file input
if ((e.which == 13 || e.which == 1 || modus == 2) || (modus == 2 ||
e.keyCode == 13)){
var new_element = document.createElement( 'input' );
new_element.type = 'text';
new_element.size = '40';

document.getElementById('tagcount').value =
parseInt(document.getElementById('tagcount').value ) + 1;

new_element.id = 'tags_element_' +
document.getElementById('tagcount').value;
var tags = document.getElementById('tagcount').value - 1;
document.getElementById('tags_element_' +
tags).parentNode.insertBefore( new_element,
document.getElementById('tags_element_' + tags) );

// Apply 'update' to element
document.getElementById('tags_element_' +
tags).multi_selector.addElement( new_element );

// Update list
document.getElementById('tags_element_' +
tags).multi_selector.addListRow(
document.getElementById('tags_element_' + tags) );
return false;
}
else{
return true;
}

};
-------------------------------------------------------------------------------------------------------------------
- addListRow creates a new row in the tag-list.
- addElement adds the new element to the multiselector-object that
contains all the input-fields
Thanks in advance!

Vega80

Oct 29 '06 #1
1 7462
ASM
vega80 a écrit :
Hi.

I have a problem with assigning an onkeypress-function to dynamically
created input-boxes.I want to put the content of an input-field
input-field = text field ? (<input type="text")
into a tag-list
I don't know what it is :-(
(a select ? an ul+li ? other ? what ?)
when the user hits enter.
where does he hits Enter ?
This works fine the first time (when
the input-field is created in a non-dynamical way). The next
input-field is created dynamically by a function that is called when
the user hits enter (the previously generated input-field will be
hidden). Then I'm trying to assign an onkeypress-function to the new
element like this:
Absolutely illegible !
try to use variables when you call several times same element.
function $(x) { return document.getElementById(x); }

// to initialize first text field :

function init() {
var count = $('tagcount').value;
$('tags_element_'+count).onkeypress = function() {
hitEnter(event);
}
$('tagcount').value++;
}

function hitEnter(e) {
e = e || event;
modus = 1;
if (
(e.which == 13 || e.which == 1 || modus == 2) ||
(modus == 2 || e.keyCode == 13)
)
newField();
}

function newField() {
var count = $('tagcount').value*1;
var field = document.createElement('input');
field.name = field.id = 'tags_element_'+count+'';
field.type = 'text';
field.size = 40;
field.onkeypress = function() { hitEnter(event); }
count--;
var target = $('tags_element_'+count+'');
target.parentNode.insertBefore(field, target);
target.multi_selector.addElement(field);
target.multi_selector.addListRow(target);
$('tagcount').value++;
}
Some sample-code below:
----------------------------------------------------------------------------------------------------------------------------------
document.getElementById('tags_element_' +
document.getElementById('tagcount').value).onkeypr ess =
function(event){
modus = 1;
e = event;

// New file input
if ((e.which == 13 || e.which == 1 || modus == 2) || (modus == 2 ||
e.keyCode == 13)){
var new_element = document.createElement( 'input' );
and where is the onkeypress for the new element ?
not seen ...
-------------------------------------------------------------------------------------------------------------------
- addListRow creates a new row in the tag-list.
- addElement adds the new element to the multiselector-object that
contains all the input-fields
when it is possible to do complicated ...
Oct 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fred Brown | last post by:
Hi, I want to cancel a certain key in JavaScript. To do so, I catch the event in OnKeyPress and cancel the default: <head> .... function f(evt) { var evt = (evt) ? evt : ((window.event) ?...
7
by: Kev | last post by:
I need to make some specific alterations to some JavaScript in webpages in order to comply with government guidelines on accessibility. Apparently, whenever the OnClick event is used, it must be...
11
by: LilAndy23 | last post by:
How can I use the onKeyPress event handler in Firefox? onKeyPress doesn't seem to fire in Firefox.
1
by: Mike Moore | last post by:
We are using the "onkeypress" event in Javascript to determine when the user hits a particular key. This works well unless an ASP.NET control (such as a textbox) has focus. In this case the...
2
by: ~toki | last post by:
How can i take the control of the key events in Class2 ? This is the code snipped that i'd tried (after try some others): public class Main : System.Windows.Forms.Form { protected virtual...
1
by: dhnriverside | last post by:
Hi I've got a form with a TextBox and an invisible button, and I've got it set so that pressing enter causes the button to click. WOrks fine, the forms submits on enter and my datalist refreshes...
2
by: Marc Robitaille | last post by:
Hello, I think it is a easy question but I don't have any clue how to do it...Is it possible to replace the char that a user type in a textbox with an other one in the OnKeyPress methode? My...
3
by: Robert Inder | last post by:
I am struggling to catch kestrokes within an Internet Explorer 6 window. My window happens to be displaying three frames, though I suspect a similar problem would arise with a single document. ...
4
by: Grant Merwitz | last post by:
Hi I am trying to implement the Microsoft Ajax.NET extensions to perform a lookup on a key press of a text box. What this will do is once a user enters a letter into the textbox, this will...
5
by: hannie | last post by:
onKeyPress does not work in Firefox. If use tab to navigate each field and press "Enter" on "Next" button to submit form and process to the next form / page, IE works well even if without onKeyPress...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.