Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically insert formfield & retrieve value

Status
Not open for further replies.

leatherback

Technical User
Oct 2, 2007
6
GB
Dear All,

My first post here. Hope you guys can help me. I am banging my head against the wall over this problem..

I am trying to create a way to insert a set of 3 (name, volume, units) new textfields into my form whenever the user needs one. (This will be triggered by an onchange() in the second-last available textfield).

In itself this works. However, somehow IE seems incapable of then finding the field, when I try to retrieve the value in that field. I had it working fine in FF, but IE killed it.

I work by copying a hidden div into the form, like so (Note that only the main js are included, and that I am flip-flopping between using ID and NAME tags):

function moreFields() {
counter++;
var newFields = document.getElementById('ingredientroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField.name
if (theName)
//newField.name = theName + counter;
newField.ID = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}

This function is succesfully called like this: <input type=\"text\" name=\"amount\" onchange=\"javascript:moreFields(); \" />

Subsequently, I am using another formfield to retrieve data suggestions from the database; For this I am modifying an XML suggest search box. The script I am working on is:

function searchSuggest(ScriptName, TheId) {
window.myValue = TheId;
if (searchReq.readyState == 4 || searchReq.readyState == 0) {
str = escape(document.getElementById(TheId).value);
searchReq.open("GET", ScriptName+'?search=' + str, true);
searchReq.onreadystatechange = handleSearchSuggest;
searchReq.send(null);
}
}

This script is succesfuly called like this: <input type=\"text\" name=\"ingredientn\" id=\"ingredients\" onkeyup=\"searchSuggest('".$ingredientSuggest."', this.id);\" autocomplete=\"off\"/>

The line: str = escape(document.getElementById(TheId).value);
is the real killer at the moment.

If somebody has something along these lines, or can help me sort this out.. Great!

Cheers,

Jelle.
 
> if (theName)
> //newField.name = theName + counter;
> newField.ID = theName + counter;
[1] First, you are better of using brace to enclose the two lines. If you uncomment the first, the second line will be executed for any theName-if that's what you want. If the first rests commented, the second is only executed if (theName) is evaluated to false-again if that is what you want.
[2] Verify if you want .ID or .id?
 
Hi Tsuji,

Thanks for your help. I did both: added backets (how DID I miss that one!?) and modified the ID to id {Silly. I should have checked my caps. Not used to working with js, I suppose}.
It seems now that it works (Or at least my lookup doesn't break anymore). Now I need to get my MoreFields to work again, because that script was adjusted just once too often ;) Sure that will give me no great pains.

Thanks a lot.

Pfew.. The world is so much brighter in the morning!

J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top