473,327 Members | 1,896 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,327 software developers and data experts.

How to read value from a text-field, create dynamic

Hi

I want to read the value of af text-field, create dynamic, in a form.
Se below a small test-site to do that (but readning fails):
I use the function Test_Read for reading the value from the dynamic create
text-field "txtName".

I thanks...
<html>

<head>

<script language="JavaScript" type="text/javascript">

function Test_Build() {
var tbody =
document.getElementById('tbllist').getElementsByTa gName("TBODY")
[0];
var row = document.createElement("TR");
var td = document.createElement("TD");
td.appendChild(document.createTextNode(""));
S = document.createElement("input");
S.setAttribute("type","text");
S.setAttribute("name","txtName");
S.setAttribute("value","Michael")
td.appendChild(S);
row.appendChild(td);
tbody.appendChild(row); }

function Test_Read() {
// alert(document.getElementById('txtName'));
// alert(document.getElementByTagName('txtName').valu e);
}

</script>

</head>

<body>
<form id="FormName">
<input type="button" value="Create a textfield" onClick="Test_Build();">
<input type="button" value="Read value from textfield"
onClick="Test_Read();">
<table id="tbllist" cellspacing="0" cellpadding="0" border="0"
width="340">
<tbody>
<tr>
<td>List of name</td>
</tr>
</tbody>
</table>
</form>
</body>

</html>

Aug 7 '08 #1
4 4851
On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gmail.comwrote:
<html>
What about a DOCTYPE?
*<head>

*<script language="JavaScript" type="text/javascript">
language="JavaScript" is deprecated and can be dropped.

Add a global variable to store the reference to the input field:

var myNameField;
*function Test_Build() {
* var tbody =
document.getElementById('tbllist').getElementsByTa gName("TBODY")
* [0];
Tables have a collection "tBodies", which is preferable to using
getElementsByTagName. Also you should make sure getElementById
actually found something:

var table = document.getElementById('tbllist');
if (table) {
var tbody = table.tBodies[0];
// ...
}
* var row = document.createElement("TR");
* var td = document.createElement("TD");
Consider using the methods insertRow() and insertCell(). That way you
don't need to append them

var row = tbody.insertRow(-1);
var td = row.insertCell(-1);
* td.appendChild(document.createTextNode(""));
* S = document.createElement("input");
* S.setAttribute("type","text");
* S.setAttribute("name","txtName");
* S.setAttribute("value","Michael")
Use the previously declared global variable here and avoid
setAttribute since it is broken in IE 6:

myNameField = document.createElement("input");
myNameField.type = "text";
myNameField.name = "txtName";
myNameField.value = "Michael";
* td.appendChild(S);
td.appendChild(myNameField);

The following appendChilds are unnecessary when using insertRow/Cell.
* row.appendChild(td);
* tbody.appendChild(row); }
*function Test_Read() {
// * alert(document.getElementById('txtName'));
// * alert(document.getElementByTagName('txtName').valu e);
* *}
You can't use getElementById nor getElementByTagName because "txtName"
is its *name* and neither its *id* nor its *tag name*.

Instead you use the reference to field from the global variable here
now:

alert(myNameField.value);

If you don't use the reference variable. you can use normal form
element reference (if you give the form a name and not an ID):

alert(document.forms["FormName"].elements["txtName"].value);
*</script>

*</head>

*<body>
* <form id="FormName">
Give the form a name if you need to reference it or its fields:

<form name="FormName">

Don't confuse id and name. They are two distinct attributes.
Unfortunately IE does consider them the same, so don't you let it
confuse you.
* <input type="button" value="Create a textfield" onClick="Test_Build();">
* <input type="button" value="Read value from textfield"
onClick="Test_Read();">
* <table id="tbllist" cellspacing="0" cellpadding="0" border="0"
width="340">
* *<tbody>
* *<tr>
* * <td>List of name</td>
* *</tr>
* *</tbody>
* </table>
* </form>
*</body>

</html>
If you want to add more that one field, you'll need to store the
references some other way, for example in an array.

Robin
Aug 7 '08 #2
alert(document.getElementById('txtName'));
This does not work because you have not set the ID for the text box

alert(document.getElementByTagName('txtName').valu e);

This does not work because getElementByTagName gets HTML tags, and
txtName is not a tag.

Add S.setAttribute("id","txtName"); in your code to set up the text
box and then use alert(document.getElementById('txtName').value);

Graham
http://cylo.co.uk
Aug 7 '08 #3
Robin Rattay wrote:
On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gmail.comwrote:
><html>

What about a DOCTYPE?
The whole thing is not Valid. Not having a DOCTYPE declaration is the least
of its problems.
[...]
You can't use getElementById nor getElementByTagName because "txtName"
is its *name* and neither its *id* nor its *tag name*.

Instead you use the reference to field from the global variable here
now:

alert(myNameField.value);

If you don't use the reference variable. you can use normal form
element reference (if you give the form a name and not an ID):

alert(document.forms["FormName"].elements["txtName"].value);
This also works with IDs in more recent user agents, as specified in W3C DOM
Level 2 HTML.
>[...]
<form id="FormName">

Give the form a name if you need to reference it or its fields:

<form name="FormName">

Don't confuse id and name. They are two distinct attributes.
Unfortunately IE does consider them the same, so don't you let it
confuse you.
And don't forget the `action' attribute, it is #REQUIRED.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 7 '08 #4
On Aug 7, 6:44*am, Robin Rattay <rot...@gmail.comwrote:
On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gmail.comwrote:
<html>

What about a DOCTYPE?
*<head>
*<script language="JavaScript" type="text/javascript">

language="JavaScript" is deprecated and can be dropped.

Add a global variable to store the reference to the input field:

var myNameField;
*function Test_Build() {
* var tbody =
document.getElementById('tbllist').getElementsByTa gName("TBODY")
* [0];

Tables have a collection "tBodies", which is preferable to using
getElementsByTagName. Also you should make sure getElementById
actually found something:

var table = document.getElementById('tbllist');
if (table) {
* *var tbody = table.tBodies[0];
* *// ...

}
* var row = document.createElement("TR");
* var td = document.createElement("TD");

Consider using the methods insertRow() and insertCell(). That way you
don't need to append them

var row = tbody.insertRow(-1);
var td = row.insertCell(-1);
* td.appendChild(document.createTextNode(""));
* S = document.createElement("input");
* S.setAttribute("type","text");
* S.setAttribute("name","txtName");
* S.setAttribute("value","Michael")

Use the previously declared global variable here and avoid
setAttribute since it is broken in IE 6:
And 7. And probably 8 too.
Aug 7 '08 #5

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

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
4
by: Randell D. | last post by:
Folks, I have a form (called FORM1) - In my INPUT submit tag, I have an onClick event that I have successfully tested/used to display the value of a TEXT box using the following function (called...
16
by: Adda | last post by:
If I cycle through the MdiChildActivate event of the parent form I can read text in a textbox on the child mdiform -- console.writeline(Me.ActiveMdiChild.Controls(1).Text) But if I have a sub...
1
by: David | last post by:
All I am using the code below in order to read an XML file in the format: <root> <selection-attr attr="p70"> <value code="Mr">Mr</value> <value code="Mrs">Mrs</value> <value...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
1
by: ashok0866 | last post by:
I had created a macro to read data from an excel sheet and write the values to a text file. I had used "ActiveSheet.Range("GB" & k).Value" command to read the values from the excel. The issue...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
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...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.