Hi,
I am trying to set the value of an input (text field) of a form that is created dynamically using createElement (The user can dynamically add as many new text areas as need, I am letting them add rows to a table).
From the suggestions of the helpful members of the forum, I have found that I can determine which text field is in focus of a form that is not dynamically created this way:
<script language=”javascript” type=”text/javascript”>
var focusElement = “title”; // default element
function changeFocus (element) {
focusElement = element;
}
function addStuff() {
var newtext =’stuff’;
document.getElementById(focusElement).value += newtext;
}
</script>
<form name=”create”>
Enter the text for row one:<br/>
<input type=”text” size=”66″ name=1 id=title onFocus=”changeFocus(‘title’);”><br/>
<input type=”text” size=”66″ id=”another” onFocus=”changeFocus(‘another’);”><br/>
<input type=”text” size=”66″ id=”yetanother” onFocus=”changeFocus(‘yetanother’);”><br/>
<input type=”button” value=”Add Text” onClick=”addStuff();”>
</form>
This code works great, but when I tried it for a text input that is created with createElement, it doesn’t seem to give the text area the value that I want it to. For some reason the code:
document.getElementsByName(focusElement).value += “stuff”;
will give me the an output of “undefined”.
This is how I dynamically create my input text boxes:
function addEvent()
{
var ni = document.getElementById(‘myDiv’);
var numi = document.getElementById(‘theValue’);
num = (document.getElementById(“theValue”).value -1)+ 2;
numi.value = num;
num = num – deleted;
var divIdName = “my”+num+”Div”;
var step =”step”+num;
var newdiv = document.createElement(‘div’);
newdiv.setAttribute(“id”,divIdName);
newdiv.innerHTML = “Step: “+num+” <INPUT TYPE=TEXT SIZE=66 NAME=” + step + ” id=” + divIdName +” onFocus=”changeFocus(‘”+ divIdName + “‘);”” +”>”;
var x = “Step: “+num+” <INPUT TYPE=TEXT SIZE=66 NAME=” + step + ” id=” + divIdName +” onFocus=”changeFocus(‘”+ divIdName + “‘);”” +”>”;
alert(x);
newdiv.innerHTML +=” <a href=”javascript:;” onclick=”removeEvent(‘”+divIdName+”‘)”><IMG SRC=images/deleteIcon.gif border=0></a>”;
ni.appendChild(newdiv);
}
Thanks in advance for any advice. ?