473,770 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript" type="text/javascript">

function Test_Build() {
var tbody =
document.getEle mentById('tblli st').getElement sByTagName("TBO DY")
[0];
var row = document.create Element("TR");
var td = document.create Element("TD");
td.appendChild( document.create TextNode(""));
S = document.create Element("input" );
S.setAttribute( "type","tex t");
S.setAttribute( "name","txtName ");
S.setAttribute( "value","Michae l")
td.appendChild( S);
row.appendChild (td);
tbody.appendChi ld(row); }

function Test_Read() {
// alert(document. getElementById( 'txtName'));
// alert(document. getElementByTag Name('txtName') .value);
}

</script>

</head>

<body>
<form id="FormName">
<input type="button" value="Create a textfield" onClick="Test_B uild();">
<input type="button" value="Read value from textfield"
onClick="Test_R ead();">
<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 4889
On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gm ail.comwrote:
<html>
What about a DOCTYPE?
*<head>

*<script language="JavaS cript" type="text/javascript">
language="JavaS cript" 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.getEle mentById('tblli st').getElement sByTagName("TBO DY")
* [0];
Tables have a collection "tBodies", which is preferable to using
getElementsByTa gName. Also you should make sure getElementById
actually found something:

var table = document.getEle mentById('tblli st');
if (table) {
var tbody = table.tBodies[0];
// ...
}
* var row = document.create Element("TR");
* var td = document.create Element("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.create TextNode(""));
* S = document.create Element("input" );
* S.setAttribute( "type","tex t");
* S.setAttribute( "name","txtName ");
* S.setAttribute( "value","Michae l")
Use the previously declared global variable here and avoid
setAttribute since it is broken in IE 6:

myNameField = document.create Element("input" );
myNameField.typ e = "text";
myNameField.nam e = "txtName";
myNameField.val ue = "Michael";
* td.appendChild( S);
td.appendChild( myNameField);

The following appendChilds are unnecessary when using insertRow/Cell.
* row.appendChild (td);
* tbody.appendChi ld(row); }
*function Test_Read() {
// * alert(document. getElementById( 'txtName'));
// * alert(document. getElementByTag Name('txtName') .value);
* *}
You can't use getElementById nor getElementByTag Name 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(myNameFie ld.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_B uild();">
* <input type="button" value="Read value from textfield"
onClick="Test_R ead();">
* <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. getElementByTag Name('txtName') .value);

This does not work because getElementByTag Name 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').valu e);

Graham
http://cylo.co.uk
Aug 7 '08 #3
Robin Rattay wrote:
On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gm ail.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 getElementByTag Name 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(myNameFie ld.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.de mon.co.uk>
Aug 7 '08 #4
On Aug 7, 6:44*am, Robin Rattay <rot...@gmail.c omwrote:
On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gm ail.comwrote:
<html>

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

language="JavaS cript" 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.getEle mentById('tblli st').getElement sByTagName("TBO DY")
* [0];

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

var table = document.getEle mentById('tblli st');
if (table) {
* *var tbody = table.tBodies[0];
* *// ...

}
* var row = document.create Element("TR");
* var td = document.create Element("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.create TextNode(""));
* S = document.create Element("input" );
* S.setAttribute( "type","tex t");
* S.setAttribute( "name","txtName ");
* S.setAttribute( "value","Michae l")

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
4296
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 while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
4
24440
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 via an onClick event) <script type="text/javascript"> function performPrePostChecks() { // myName being an iput text box
16
453
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 in the Parent form and reference a child mdi form like this: Sub ReadText()
1
4509
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 code="Ms">Ms</value> <value code="Miss">Miss</value>
35
11493
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
0
4753
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 the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.
1
22193
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 is: some cells in the excel sheet is having two lines value and four lines data, the text file is generating that value in a single line. (Ex. the cell value in address column is in four lines and should display in four lines in the text file...
7
3059
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 2718 2895 I wanna to read the data to an array, as the follows:
6
418
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 set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
5
3466
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: default.aspx:
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.