473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stuck on how to access a textfield added in a div

Ed
Hi,

I'm stuck at the moment trying to work out how to access the value of a
textfield which gets added in a DIV element.

I have functions to add and remove the textfields from the elements as
shown here:

function addListen(){
var ni = document.getEle mentById('liste ndiv');
var numi = document.getEle mentById('liste ndivval');
var num = (document.getEl ementById("list endivval").valu e -1)+
2;
numi.value = num;
var divIdName = "listen"+nu m;
var newdiv = document.create Element('div');
newdiv.setAttri bute("id",divId Name);
var htmlstr="<td><i nput type=text id="+divIdName+ "
name="+divIdNam e+" size=21 maxlength=21 />&nbsp;";
htmlstr=htmlstr +"<input type=button value=\" -\"
onclick=\"javas cript:removeLis ten(\'" + divIdName + "\');\">&nbsp;" ;
htmlstr=htmlstr +"<input type=button value=\" +\"
onclick=\"javas cript:addListen ();\" /></td>";
newdiv.innerHTM L=htmlstr;
ni.appendChild( newdiv);
}

//remove a listen textfield in the apache config form
function removeListen(di vNum){
var d = document.getEle mentById('liste ndiv');
var olddiv = document.getEle mentById(divNum );
d.removeChild(o lddiv);
}

And I'm trying to write a function here to verify the contents of the
new textfields added:

//check listen values
function checklistens(di vNum){
var num = document.getEle mentById("liste ndivval").value ;
var lnum= "listen"+nu m;
var numi = document.getEle mentById(lnum). value;
alert("numi="+n umi);
var numi = document.getEle mentById(lnum). focus;

return false;
}

However I keep getting an undefined object in the alert. Can anyone see
what I'm doing wrong please?

Thanks,

Ed.

Oct 30 '05 #1
2 2348
Ed wrote:
Hi,

I'm stuck at the moment trying to work out how to access the value of a
textfield which gets added in a DIV element.

I have functions to add and remove the textfields from the elements as
shown here:

function addListen(){
var ni = document.getEle mentById('liste ndiv');
var numi = document.getEle mentById('liste ndivval');
var num = (document.getEl ementById("list endivval").valu e -1)+
2;
Rather than using subtraction to ensure num is a number, use unary
'+'. There also seems little point in using numi:

var num = +document.getEl ementById("list endivval").valu e+1;

numi.value = num;
Unless you are going to use numi somewhere else, just do:

document.getEle mentById('liste ndivval') = num;

var divIdName = "listen"+nu m;
var newdiv = document.create Element('div');
newdiv.setAttri bute("id",divId Name);
Why not access the attribute directly:

newdiv.id = 'listen' + num;

var htmlstr="<td><i nput type=text id="+divIdName+ "
name="+divIdNam e+" size=21 maxlength=21 />&nbsp;";
I'm not sure what this line was before it was wrapped, but if there
was no space between divIdName and the 'name' attribute then that will
likely cause a problem. It's always a good idea to quote attribute
values even it it isn't strictly needed.

You have also given the input the same ID as the div which is invalid
HTML and will cause problems.

You are using innerHTML to add part of a table, and adding it to a div
which again is invalid HTML.

Do not allow posted code to be automatically wrapped, manually wrap it
at about 70 characters.

htmlstr=htmlstr +"<input type=button value=\" -\"
htmlstr += "<input type=button value=\" -\"

onclick=\"javas cript:removeLis ten(\'" + divIdName + "\');\">&nbsp;" ;
There is no need for 'javascript:' in the value of an onclick attribute.

Your button can be:

var oBut = document.create Element('input' );
oBut.type = 'button';
oBut.value = '-';
oBut.onclick = function() {removeListen(n ewdiv);};
newdiv.appendCh ild(oBut);

Untested, but newdiv creates a closure that will refer back to the div
you are creating, even after the function has ended.

htmlstr=htmlstr +"<input type=button value=\" +\"
onclick=\"javas cript:addListen ();\" /></td>";
newdiv.innerHTM L=htmlstr;
I'm not sure what most browsers will do with this, but it is invalid
HTML. You should never use innerHTML to write parts of tables, either
write the entire table or just cell contents.

If you are modifying tables, use the DOM.

The same comments for the remove button apply to the add button.

ni.appendChild( newdiv);
}

//remove a listen textfield in the apache config form
function removeListen(di vNum){
var d = document.getEle mentById('liste ndiv');
var olddiv = document.getEle mentById(divNum );
d.removeChild(o lddiv);


You could do:

var olddiv = document.getEle mentById(divNum );
olddiv.parentNo de.removeChild( olddiv);

Maybe when the above is fixed your other functions may work...

[...]
--
Rob
Oct 30 '05 #2
Ed
Hi Rob,

Many thanks for your detailed reply. I have updated my code as per your
suggestions, and it works now. The only snag I have is when I click on
a link on the page and then go back, my div element has vanished, and I
have to enter the form values again.

Any ideas how I could get round this?

Thanks,

Ed.

<code>
//add another listen textfield in the apache config form
function addListen(){
var olddiv = document.getEle mentById('liste ndiv');
var num = +document.getEl ementById("list endivval").valu e+1;
document.getEle mentById('liste ndivval').value = num;
var divIdName = "listen"+nu m;

var newdiv = document.create Element('div');
newdiv.id = divIdName;

//add text box
var otext = document.create Element('input' );
otext.type = 'text';
otext.size=21;
otext.maxlength =21;
newdiv.appendCh ild(otext);
//add space
var onbsp = document.create TextNode(" ");
newdiv.appendCh ild(onbsp);

//add minus button
var oBut = document.create Element('input' );
oBut.type = 'button';
oBut.value = '-';
oBut.onclick = function() {removeListen(d ivIdName);};
newdiv.appendCh ild(oBut);

//add plus button
oBut = document.create Element('input' );
oBut.type = 'button';
oBut.value = '+';
oBut.onclick = function() {addListen();};
newdiv.appendCh ild(oBut);

olddiv.appendCh ild(newdiv);
}

//remove a listen textfield in the apache config form
function removeListen(di vNum){
var d = document.getEle mentById('liste ndiv');
var olddiv = document.getEle mentById(divNum );
d.removeChild(o lddiv);
}

//check listen values
function verifyListens() {
var d=document.apac heconfig;
var i=0;
var listenPattern=/^listen/;
for(;d[i]!=null;i++){
if( d[i].name.match(lis tenPattern)){
if(!verifyListe nValue(d[i])){
d[i].focus();
return false;
}
}
}
}
</code>

Oct 30 '05 #3

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

Similar topics

2
2287
by: C. Armour | last post by:
Help me, I'm suffering! Situation: I have a load() function which loads a bunch of TextFields with values. There are TextListeners registered for each of these TextFields. Thus, changing the text in any of them fires off a TextEvent, even if done programmatically (by means of setText()). The problem: I need all the various TextEvents...
1
1796
by: Johnny Funder | last post by:
Hi, I'm working on a GUI using many textfields. Which textfield to put data into depends on data received via a telnet session. To avoid doing a huge block of if/else if/else if I wondered if it is possible to access objects indrectly. What I'm looking for is something like this (simplified): I have a textfield : jTextFieldBRI21 = new...
3
4969
by: Mats | last post by:
It's good practice to validate input, not only where it should be coming from, but from anywhere it's possible to change or add input for a "client". If all user input is transfered using "post" you can be pretty tough on querystrings, if you use them at all. But user input could have a name like Mc'Donald, and we would not like quotes...
2
1336
by: Rob | last post by:
Hi everyone, I have a form that has several disabled text fields. In these text fields, I have initial values. Next to each text field lies a "copy" button. When clicked, the content of the disabled text field is appended to the content of another textfield, the only one that isn't disabled (therefore the user can copy content from the other...
1
3167
by: Suresh | last post by:
How do I insert a char into the textfield on the keypress event of another textfield? I have two textboxes, "text1" and "text2". Important: "text1" is outside the table. "text2" is inside the table cell. On the keydown/keypress/keyup event of "text1", the character which I entered in the "text1" must display in the textfield "text2".
7
6266
by: damjanu | last post by:
Hi All; I need little help. I have a datasheet form. I allow user to do 'filter by selection'. My form contains a column with values. As user changes selections, I want to calculate totals. I can do this the first time the form loads.
1
1427
by: Briton | last post by:
Hi I am using Access and want to do som SQL in the event builder. I got a Form with a textfield named "Number" and another named "Name". When the user type a number in "Number" i should start the After_update event. Here it should look in a table "T_Errors" if the number is there. If it's not it should show a msgBox "Try another number"...
0
1906
by: Mark Phillips | last post by:
Hello, I am having a problem in which a file can get stuck open when a thread that is attempting to write to it gets aborted (ThreadAbortedException occurs). The log file gets stuck open until the process is shut down (cannot delete file from Windows Explorer, for example). My test application stops execution with: An unhandled exception...
0
1758
by: adamselearning | last post by:
Hi... I've followed Colin Moock's AS3 notes to create a VirtualPet I've got it working from the code at: http://www.adobe.com/devnet/actionsc...n_moock_f6.pdf ...but am now trying to add a textField on the stage that will display the % of calories remaining. Extra code I have created in VirtalPet.as: 1. private var petView (An instance...
0
7435
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...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
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...
1
5361
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.