Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding script to document.createElement()

    I haven't been able to get any help with this, which is a little frustrating. I have a script which adds fields to a form . . . however many are needed according to the user. This bit of code adds the ingredient field.


    Code:
    var cellRight = row.insertCell(3); //this shows it's the third cell in the row
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'Ingredient' + iteration;
    el.id = 'Ingredient' + iteration;
    el.size = 60;
    cellRight.appendChild(el);
    I need to be able to add some javascript to that input field and I can't figure out how to do it. There's got to be a way . . .

    When the form field is written into the page it looks like this:

    <input type=text id=ingredient1 name=ingredient1 size=60>

    I need it to look like this:

    <input type="text" name="Ingredient1" id="Ingredient1" size="60" onKeyPress="return addNtabit(this,event)">

    How do I add the script to the create.element so it shows up in the field when the field is added to the page?

    I've got it sending the code to the page, just not in the right place.

    Code:
    var cellRight = row.insertCell(3);
    var el = document.createElement('input');
    var txtCode = document.createTextNode('onKeyPress="return addNtabit(this,event)"');
    el.type = 'text';
    el.name = 'Ingredient' + iteration;
    el.id = 'Ingredient' + iteration;
    el.size = 60;
    cellRight.appendChild(el);
    cellRight.appendChild(txtCode);
    This produces:

    <input type="text" name="Ingredient1" id="Ingredient1" size="60"> onKeyPress="return addNtabit(this,event)"

    Help? Everything else I've tried doesn't even get me close.

    Thanks

    HeadElf

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    var cellRight = row.insertCell(3); //this shows it's the third cell in the row
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'Ingredient' + iteration;
    el.id = 'Ingredient' + iteration;
    el.size = 60;
    cellRight.appendChild(el);
    el.onkeypress=function(event){ return addNtabit(this,event); }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Vic, you truly walk on water. I have been fighting this for two days, asking for help in another forum and getting nothing, not even a reply. You cannot know how much I appreciate your help.

    My sincerest thanks.

    HeadElf


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •