...

View Full Version : Adding script to document.createElement()



HeadElf
06-24-2006, 10:14 PM
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.




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.



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

vwphillips
06-24-2006, 10:47 PM
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); }

HeadElf
06-25-2006, 02:40 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum