views:

1211

answers:

5

I'm trying to dynamically add some textboxes (input type=text) to a page in javascript and prefill them. The textboxes are coming up, but they are coming up empty. What is the proper way to pre-fill a textbox. Ideally I'd love to use the trick of creating a child div, setting the innerhtml property, and then adding that div to the parent main div but that didn't work. Then I thought I'd use the dom but setting textboxname.value before or after insertion won't work and doing txttextbox.setattribute('value','somevalue') won't work either. Works fine in firefox. What gives? This has to be possible? Here is my code. I know I'm only using string literals, but these will be replaced with the results of a web service call eventually. Below is some code. Oh and how do you format code to show up as you type it? I thought it said to indent four spaces, and I did that but the code is still on one line. Sorry about that.

var table=document.createElement('table');
var tbody=document.createElement('tbody');
var row=document.createElement('tr');
row.appendChild(document.createElement('td').appendChild(document.createTextNode('E-mail')));
var txtEmail=document.createElement('input');

row.appendChild(document.createElement('td').appendChild(txtEmail));
tbody.appendChild(row);
table.appendChild(tbody);

//document.getElementById('additionalEmails').innerHTML="";
document.getElementById('additionalEmails').appendChild(table);
+4  A: 

txtEmail.value = 'my text'

Does not work?

StingyJack
+2  A: 

You can also use Prototype to do this easily:

document.body.insert(new Element("input", { type: "text", size:20, value:'hello world' }))
Diodeus
A: 

I've encountered problems similar to this in the past, and while my memory is a bit fuzzy on why it was happening exactly, I think you may need to make sure the element is actually added to the DOM before modifying its value. e.g:


var txtEmail=document.createElement('input');

document.getElementById('someElementThatAlreadyExists').appendChild(txtEmail);

txtEmail.value = 'sample text';

mmacaulay
I thought that by adding it to a cell which was being added to a row which was being added to a table that was being added to the current div that would be adding it to the document? I'm trying to dynamically generate a table, and it's proving much more difficult than expected.
Chris Westbrook
I feel your pain. Like I said, I can remember having a problem similar to this in the past, but I can't remember what I did to solve it. What I'm suggesting is that you try to set the value of the input after the dynamically created table and all of its children have been added to the div.
mmacaulay
Another thought - I also remember having problems with code that mixes creating DOM elements via createElement/appendChild and inserting inline HTML with someElement.innerHTML. Sorry I couldn't be of more help :/
mmacaulay
To some extent, this behavior may be browser-specific; I pre-fill elements created dynamically all the time before actually adding them to the DOM tree and it works just fine. IE 6 has some strange problems with various form elements, but not textboxes AFAIK.
Jason Bunting
A: 

I ended up solving this problem by injecting the html directly into a page via a child div. That did work, it's just that I am blind and the software I use to read the screen for some stupid reason failed to see the text in the textbox. Go figure. Thanks for the tip on prototype though, if I ever decide not to cheat and add the eleements to the dom directly, I'll do it that way.

Chris Westbrook
A: 

thanks for such a gr8 news, hope to listen from you again

web 2.0 development

web 2.0