/    Sign up×
Community /Pin to ProfileBookmark

Why doesn’t my code for document.getElementById work?

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. ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJul 22.2006 — document.getElementsByName(focusElement).value += "stuff";[/quote]
This returns an array, not one object. You might try:
document.getElementsByName(focusElement)[0].value += "stuff";
Copy linkTweet thisAlerts:
@tbroasauthorJul 22.2006 — Thanks for the suggestion, but it didn't seem to work for me. From testing I know that I am correctly finding out what new field is onFocus, but for some reason I cannot set it's value. Is it not possible to set the value of a field that is created with createElement?
Copy linkTweet thisAlerts:
@konithomimoJul 22.2006 — create your inputs the same way that you created your divs, and then just append them. make sure to set whatever attributes you need to. Sometimes when you create elements the way that you are trying to you cannot change the attirbutes unless they were already defined in their creation. For example, sometimes you cannot get the width of an object unless you declared the width in the element's tag.
×

Success!

Help @tbroas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...