473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

adding an input element

I have this code that adds a table row and within the cells I create
some input elements. When I go back using javascript the functionaliuty
is seeing the form element. Anyone tell why I can see them?

<form name='update'>

myTR=document.createElement("TR");
myTR.setAttribute("id","RESULTS4");
// create cell 1 and content
myTD=document.createElement("TD");
//create input items
myINPUT=document.createElement("INPUT");
myINPUT.setAttribute("type","text");
myINPUT.setAttribute("readonly");
myINPUT.setAttribute("className","grey");
myINPUT.setAttribute("name","NAME4");
myINPUT.setAttribute("maxlength","35");
myINPUT.setAttribute("size","35");
myINPUT.setAttribute("value","somevalue");
//append the input to the cell
myTD.appendChild(myINPUT);
myTR.appendChild(myTD);

So, if when I call the input value the element is not found like:
temp1 = document.update.elements["NAME4"].value;
alert(temp1);

The error message is: "null or not an object". Somehow this element is
not getting added to the form "update".

Any help is appreciated.

Mike

Jul 20 '05 #1
6 1982


Michael Hill wrote:
I have this code that adds a table row and within the cells I create
some input elements. When I go back using javascript the functionaliuty
is seeing the form element. Anyone tell why I can see them?

<form name='update'>

myTR=document.createElement("TR");
myTR.setAttribute("id","RESULTS4");
// create cell 1 and content
myTD=document.createElement("TD");
//create input items
myINPUT=document.createElement("INPUT");
myINPUT.setAttribute("type","text");
myINPUT.setAttribute("readonly");
myINPUT.setAttribute("className","grey");
myINPUT.setAttribute("name","NAME4");
myINPUT.setAttribute("maxlength","35");
myINPUT.setAttribute("size","35");
myINPUT.setAttribute("value","somevalue");
//append the input to the cell
myTD.appendChild(myINPUT);
myTR.appendChild(myTD);

So, if when I call the input value the element is not found like:
temp1 = document.update.elements["NAME4"].value;
alert(temp1);

The error message is: "null or not an object". Somehow this element is
not getting added to the form "update".

Any help is appreciated.

Mike


Additional information ...... In fact when I use:

for ( var i=0; i<document.update.elements.length; i++ )
{
if ( document.update.elements[i].name.substr(0,4) == "NAME" )
{
alert(document.update.elements[i].name);
}
}
I see the first 3 elements that were there when the html was executed,
including the element "NAME4" using this for loop.

alert("3 " + document.update.elements["NAME3"].value ); produces valid
data
alert("4 " + document.update.elements["NAME4"].value ); error message
null or not an object

Jul 20 '05 #2
DU
Michael Hill wrote:

Michael Hill wrote:

I have this code that adds a table row and within the cells I create
some input elements. When I go back using javascript the functionaliuty
is seeing the form element. Anyone tell why I can see them?

<form name='update'>

myTR=document.createElement("TR");
myTR.setAttribute("id","RESULTS4");
Browsers overall have a better support for assigning values to specific
attributes than when resorting to a general-purpose method like
setAttribute. This is confirmed by other reviewers too.

"General rule: don't use setAttribute() when a better way of setting the
attribute is available" Peter-Paul Koch
http://www.quirksmode.org/dom/w3c_core.html#attributes

So, here, just

myTR.id = "RESULTS4";
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-63534901
// create cell 1 and content
myTD=document.createElement("TD");
//create input items
myINPUT=document.createElement("INPUT");
myINPUT.setAttribute("type","text");
myINPUT.type = "text";
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-62883744
myINPUT.setAttribute("readonly");
Here, setAttribute must take 2 parameters and you only provide one. So,
I think this won't succeed.
Instead,
myINPUT.disabled = true;
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-50886781

myINPUT.setAttribute("className","grey");
myINPUT.className = "gray";
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-95362176
myINPUT.setAttribute("name","NAME4");
myINPUT.setAttribute("maxlength","35");
myINPUT.setAttribute("size","35");
myINPUT.setAttribute("value","somevalue");
//append the input to the cell
myINPUT.name = "NAME4";
myINPUT.maxLength = 35;
// attribute long maxLength;
myINPUT.size = 35;
// Modified in DOM Level 2:
// attribute unsigned long size;
myINPUT.value = "somevalue";
myTD.appendChild(myINPUT);
Right here, before inserting the myINPUT object, you may want to "alert"
all of its properties to see if everything went well.
myTR.appendChild(myTD);

So, if when I call the input value the element is not found like:
temp1 = document.update.elements["NAME4"].value;
alert(temp1);

The error message is: "null or not an object". Somehow this element is
not getting added to the form "update".

Any help is appreciated.

Mike

Additional information ...... In fact when I use:

for ( var i=0; i<document.update.elements.length; i++ )
{
if ( document.update.elements[i].name.substr(0,4) == "NAME" )
{
alert(document.update.elements[i].name);
}
}
I see the first 3 elements that were there when the html was executed,
including the element "NAME4" using this for loop.

alert("3 " + document.update.elements["NAME3"].value ); produces valid
data
alert("4 " + document.update.elements["NAME4"].value ); error message
null or not an object


Then that means you also have a problem with the number of iterations or
the dynamically created inputs have not been integrated into the
document yet at the memory level. I can not see your whole code, so
impossible to say.

Your for loop is supposed to iterate the alert "i + 1" times. I
recommend appending all the info into 1 string which you can "alert" at
the end of the for loop.

Also, you would need to provide the browser and browser version in which
the problem happens.

DU
Jul 20 '05 #3


Michael Hill wrote:
I have this code that adds a table row and within the cells I create
some input elements. When I go back using javascript the functionaliuty
is seeing the form element. Anyone tell why I can see them?

<form name='update'> myINPUT=document.createElement("INPUT");
myINPUT.setAttribute("type","text");
myINPUT.setAttribute("readonly");
myINPUT.setAttribute("className","grey");
myINPUT.setAttribute("name","NAME4");
myINPUT.setAttribute("maxlength","35");
myINPUT.setAttribute("size","35");
myINPUT.setAttribute("value","somevalue");
//append the input to the cell
myTD.appendChild(myINPUT);
myTR.appendChild(myTD);

So, if when I call the input value the element is not found like:
temp1 = document.update.elements["NAME4"].value;
alert(temp1);

The error message is: "null or not an object". Somehow this element is
not getting added to the form "update".


Is that IE on Windows? Unfortunately with that browser you would need to use
var myINPUT = document.createElement(
'<input type="text" name="NAME4">'
)
if you want to address the input later by its name in the elements
collection.
Or as long as you have unique names I suggest to use
var myINPUT = document.createElement('input');
myINPUT.id = myINPUT.name = 'NAME4';
then IE should be able to address
document.formName.elements['NAME4']
later
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4


Martin Honnen wrote:
Michael Hill wrote:
I have this code that adds a table row and within the cells I create
some input elements. When I go back using javascript the functionaliuty
is seeing the form element. Anyone tell why I can see them?

<form name='update'>

myINPUT=document.createElement("INPUT");
myINPUT.setAttribute("type","text");
myINPUT.setAttribute("readonly");
myINPUT.setAttribute("className","grey");
myINPUT.setAttribute("name","NAME4");
myINPUT.setAttribute("maxlength","35");
myINPUT.setAttribute("size","35");
myINPUT.setAttribute("value","somevalue");
//append the input to the cell
myTD.appendChild(myINPUT);
myTR.appendChild(myTD);

So, if when I call the input value the element is not found like:
temp1 = document.update.elements["NAME4"].value;
alert(temp1);

The error message is: "null or not an object". Somehow this element is
not getting added to the form "update".


Is that IE on Windows? Unfortunately with that browser you would need to use
var myINPUT = document.createElement(
'<input type="text" name="NAME4">'
)
if you want to address the input later by its name in the elements
collection.
Or as long as you have unique names I suggest to use
var myINPUT = document.createElement('input');
myINPUT.id = myINPUT.name = 'NAME4';
then IE should be able to address
document.formName.elements['NAME4']
later
--

Martin Honnen
http://JavaScript.FAQTs.com/


Thanks DU amd Martin for your help. I have implemented both your suggestions.

The browser is IE 5.5 and 6.0.

Finding the element didn't work using:

myINPUT.name = 'NAME4';

until I also made the change to:

myINPUT.id = myINPUT.name = 'NAME4';

then it did. Seems cludgy, but it works,

If now I want to remove a row that I had previously identified it as:

myTR = document.createElement("TR");
myTR.id = "MYROW4";

How would I delete this one row? I know I can delete all the rows in hte table
by using this:

var rows = document.getElementById("list").rows;
while ( rows.length > 0 )
{
var mytr = rows[0];
mytr.parentNode.removeChild(mytr);
}


Jul 20 '05 #5


Michael Hill wrote:

If now I want to remove a row that I had previously identified it as:

myTR = document.createElement("TR");
myTR.id = "MYROW4";

How would I delete this one row?


You delete any node with
nodeObject.parentNode.removeChild(nodeObject)
thus if you still have access to the variable myTR
myTR.parentNode.removeChild(myTR)
In general you should check
if (myTR.parentNode && myTR.parentNode.removeChild)
before calling the method.
If you don't have acess to the variable myTR any longer then use
document.getElementById to find the row:
var myTR;
if (document.getElementById) {
myTR = document.getElementById('MYROW4');
if (myTR.parentNode && myTR.parentNode.removeChild) {
myTR.parentNode.removeChild(myTR);
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #6


Michael Hill wrote:
Part 1.1 Type: Plain Text (text/plain)
Encoding: 7bit


I got it:

var rows = document.getElementById("list");
var temp = document.getElementById("NAME4");
rows.removeChild(temp);

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
1
by: emperor-laban | last post by:
How can I add more <input type="file" /> -fields by the click of a button? The idea is that the clients should be able to upload a number of files from the same form, but I shouldn't need to know...
9
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
4
by: glebur | last post by:
Hi, I'm trying to create a web service client in C# but I get stuck at one of the first steps. When adding a Web reference to the Visual Studio project; I get this error (this is a translation,...
2
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
6
by: Clive Backham | last post by:
I need to set up forms where some input fields are mandatory and others are optional. A common convention is to place an asterisk on the labels of the mandatory fields. Of course, I can just hard...
0
by: vamsioracle | last post by:
Hi all I have done a setup for a new absence type. Now my client wants to add balance to the existing leave balance. So i created an element with the following details Element Name: CTO leave...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.