Assign onkeypress-function to dynamically created input-boxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vega80

    #1

    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.getEle mentById('tags_ element_' +
    document.getEle mentById('tagco unt').value).on keypress = function(event)

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

    Some sample-code below:
    ----------------------------------------------------------------------------------------------------------------------------------
    document.getEle mentById('tags_ element_' +
    document.getEle mentById('tagco unt').value).on keypress =
    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.create Element( 'input' );
    new_element.typ e = 'text';
    new_element.siz e = '40';

    document.getEle mentById('tagco unt').value =
    parseInt(docume nt.getElementBy Id('tagcount'). value) + 1;

    new_element.id = 'tags_element_' +
    document.getEle mentById('tagco unt').value;
    var tags = document.getEle mentById('tagco unt').value - 1;
    document.getEle mentById('tags_ element_' +
    tags).parentNod e.insertBefore( new_element,
    document.getEle mentById('tags_ element_' + tags) );

    // Apply 'update' to element
    document.getEle mentById('tags_ element_' +
    tags).multi_sel ector.addElemen t( new_element );

    // Update list
    document.getEle mentById('tags_ element_' +
    tags).multi_sel ector.addListRo w(
    document.getEle mentById('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

  • ASM

    #2
    Re: Assign onkeypress-function to dynamically created input-boxes

    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.getEle mentById(x); }

    // to initialize first text field :

    function init() {
    var count = $('tagcount').v alue;
    $('tags_element _'+count).onkey press = function() {
    hitEnter(event) ;
    }
    $('tagcount').v alue++;
    }

    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').v alue*1;
    var field = document.create Element('input' );
    field.name = field.id = 'tags_element_' +count+'';
    field.type = 'text';
    field.size = 40;
    field.onkeypres s = function() { hitEnter(event) ; }
    count--;
    var target = $('tags_element _'+count+'');
    target.parentNo de.insertBefore (field, target);
    target.multi_se lector.addEleme nt(field);
    target.multi_se lector.addListR ow(target);
    $('tagcount').v alue++;
    }
    Some sample-code below:
    ----------------------------------------------------------------------------------------------------------------------------------
    document.getEle mentById('tags_ element_' +
    document.getEle mentById('tagco unt').value).on keypress =
    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.create Element( '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 ...

    Comment

    Working...