473,656 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

form input elements and label. IE problem

Hi

I have small function to generate my form controls:

function buildInput(sTyp e, vValue, vId, sName, sLabel){
var oInput = null;
var oLabel = document.create Element('label' );
var oCont = document.create Element('span') ;
var oText = document.create TextNode(sLabel );
oInput = document.create Element('<input type="'+ sType +'" name="'+ sName +'" />');
oInput.id = vId;
oInput.value = vValue;
oLabel.appendCh ild(oText);
oLabel.setAttri bute('for', vId);
oCont.appendChi ld(oInput);
oCont.appendChi ld(oLabel);
return oCont;
}

The function is generating code like this:

<SPAN><INPUT id="1062" type="radio" name="mainIntr" value="Televisi on" /><LABEL
for="1062">Tele vision</LABEL></SPAN>

The controls are generated OK but when i click the label next to radio button it's not selecting it.
On the other hand when i hard code the same code in my HTML file i can select the radio button by
clicking the label next to it.

How can I generate this code so it works?

PS: i have no problems with FF only IE.

Thank you
--

Ralph
Jan 22 '07 #1
3 5633
Ralph wrote:
Hi

I have small function to generate my form controls:

function buildInput(sTyp e, vValue, vId, sName, sLabel){
var oInput = null;
var oLabel = document.create Element('label' );
var oCont = document.create Element('span') ;
var oText = document.create TextNode(sLabel );
oInput = document.create Element('<input type="'+ sType +'" name="'+
sName +'" />');
Why create the element like this rather than create the input and then
set the attributes?

--
Ian Collins.
Jan 22 '07 #2
Ralph wrote:
Hi

I have small function to generate my form controls:

function buildInput(sTyp e, vValue, vId, sName, sLabel){
var oInput = null;
var oLabel = document.create Element('label' );
var oCont = document.create Element('span') ;
var oText = document.create TextNode(sLabel );
oInput = document.create Element('<input type="'+ sType +'" name="'+ sName +'" />');
That is IE's special method of adding element, it is not compliant with
the W3C DOM Core specification. The argument to the
document.create Element() method must be a tag name, not a string of
markup. To be standards compliant, replace the above line with:

oInput = document.create Element('input' );
oInput.name = sName;
oInput.type = sType;

However, it will still not work properly in IE, which is seriously
broken when it comes to adding form controls dynamically - one reason
is because it refuses to properly add the name attribute. Try this
thread with subject "Cross Browser Problem - IE can not find a dynamic
form" from April 2006:

<URL:
http://groups.google.co.uk/group/com...2cbda3521fab6e
>
Also, in this case the radio buttons are unselectable (probably because
of the name issue).

The usual trick is to add controls in the HTML and manage them
dynamically by hiding or showing and disabling or enabling them as
appropriate.

oInput.id = vId;
oInput.value = vValue;
oLabel.appendCh ild(oText);
oLabel.setAttri bute('for', vId);
oCont.appendChi ld(oInput);
oCont.appendChi ld(oLabel);
return oCont;
}

The function is generating code like this:

<SPAN><INPUT id="1062" type="radio" name="mainIntr" value="Televisi on" /><LABEL
for="1062">Tele vision</LABEL></SPAN>
To be valid HTML, an id must not start with a number - but that is
irrelevant to the issue. :-)

Incidentally, you may as well drop the faux XHTML markup, IE doesn't
understand it. Just use HTML 4.01 strict.

>
The controls are generated OK but when i click the label next to radio button it's not selecting it.
On the other hand when i hard code the same code in my HTML file i can select the radio button by
clicking the label next to it.

How can I generate this code so it works?
You can try innerHTML, an ugly hack that you should thoroughly test,
but it seems to work in IE, Firefox and Opera. The labels still won't
work properly in IE:

function buildInput(sTyp e, vValue, vId, sName, sLabel)
{
var oLabel = document.create Element('label' );
var oCont = document.create Element('span') ;
oCont.appendChi ld(oLabel);
oLabel.innerHTM L = '<input type=' + sType + ' value="' + vValue +
'" ' + ' id="' + vId + '" ' + ' name="' + sName + '">' + sLabel;
oLabel.setAttri bute('for', vId);

return oCont;

}

If this is for general use, you'll need to make some of those
attributes conditional on whether they've been supplied or not (e.g.
you may not want a label every time you generate an input element).

PS: i have no problems with FF only IE.
The original code doesn't work at all in Firefox, it just errors out
because Firefox doesn't support the use of markup as an argument to
createElement() .
--
Rob

Jan 22 '07 #3
RobG wrote:
Ralph wrote:
>Hi

I have small function to generate my form controls:

function buildInput(sTyp e, vValue, vId, sName, sLabel){
var oInput = null;
var oLabel = document.create Element('label' );
var oCont = document.create Element('span') ;
var oText = document.create TextNode(sLabel );
oInput = document.create Element('<input type="'+ sType +'" name="'+ sName +'" />');

That is IE's special method of adding element, it is not compliant with
the W3C DOM Core specification. The argument to the
document.create Element() method must be a tag name, not a string of
markup. To be standards compliant, replace the above line with:

oInput = document.create Element('input' );
oInput.name = sName;
oInput.type = sType;

However, it will still not work properly in IE, which is seriously
broken when it comes to adding form controls dynamically - one reason
is because it refuses to properly add the name attribute. Try this
thread with subject "Cross Browser Problem - IE can not find a dynamic
form" from April 2006:

<URL:
http://groups.google.co.uk/group/com...2cbda3521fab6e

Also, in this case the radio buttons are unselectable (probably because
of the name issue).

The usual trick is to add controls in the HTML and manage them
dynamically by hiding or showing and disabling or enabling them as
appropriate.

> oInput.id = vId;
oInput.value = vValue;
oLabel.appendCh ild(oText);
oLabel.setAttri bute('for', vId);
oCont.appendChi ld(oInput);
oCont.appendChi ld(oLabel);
return oCont;
}

The function is generating code like this:

<SPAN><INPUT id="1062" type="radio" name="mainIntr" value="Televisi on" /><LABEL
for="1062">Tel evision</LABEL></SPAN>

To be valid HTML, an id must not start with a number - but that is
irrelevant to the issue. :-)

Incidentally, you may as well drop the faux XHTML markup, IE doesn't
understand it. Just use HTML 4.01 strict.

>The controls are generated OK but when i click the label next to radio button it's not selecting it.
On the other hand when i hard code the same code in my HTML file i can select the radio button by
clicking the label next to it.

How can I generate this code so it works?

You can try innerHTML, an ugly hack that you should thoroughly test,
but it seems to work in IE, Firefox and Opera. The labels still won't
work properly in IE:

function buildInput(sTyp e, vValue, vId, sName, sLabel)
{
var oLabel = document.create Element('label' );
var oCont = document.create Element('span') ;
oCont.appendChi ld(oLabel);
oLabel.innerHTM L = '<input type=' + sType + ' value="' + vValue +
'" ' + ' id="' + vId + '" ' + ' name="' + sName + '">' + sLabel;
oLabel.setAttri bute('for', vId);

return oCont;

}

If this is for general use, you'll need to make some of those
attributes conditional on whether they've been supplied or not (e.g.
you may not want a label every time you generate an input element).

>PS: i have no problems with FF only IE.

The original code doesn't work at all in Firefox, it just errors out
because Firefox doesn't support the use of markup as an argument to
createElement() .

Thank you for taking time to respond to my post. finally i got everything working in IE as well as
in FF. This is the code I'm using now:

function buildInputEl(sT ype, vValue, vId, sName, sLabel){
var oInput = null;
var oLabel = null;
var oText = document.create TextNode(sLabel );
try{
oInput = document.create Element('<input type="'+ sType +'" name="'+ sName +'" />');
oLabel = document.create Element('<label for="'+vId+'"></label>');
}catch(err){
oInput = document.create Element('input' );
oInput.setAttri bute('type', sType);
oInput.setAttri bute('name', sName);
oLabel = document.create Element('label' );
}
oInput.id = vId;
oInput.value = vValue;
oLabel.appendCh ild(oInput);
oLabel.appendCh ild(oText);
return oLabel;
}

I hope it helps.
--
Ralph
Jan 23 '07 #4

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

Similar topics

1
9982
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a number. That number will appear in a seperate box at the bottom. So basically whatever you choose has a corresponding number associated with it (except for the text input, which you enter whatever number) and those numbers are added and produced in...
9
2245
by: Rick Cook | last post by:
I would like to do a response form on one of my sites instead of posting an e-mail address for spammers. I've taken a couple of examples from sites, but the code looks clunky and non-conformant to my inexperienced eye. Also, on the sites I've found you can get the e-mail address simply by viewing the page source. It seems to me there should be a way to hide that, but as I say I'm hardly an expert. Can anyone recommend a good site to...
4
2135
by: pizzy | last post by:
INTRO: I tried to clean it up for easy reading. I hope I didn't make any mistakes. PROBLEM: WOW, this is some crazy sh!t. I can't get my checkbox (see "TAGSELECTED") to print my textboxes (see "TAG#") when more than 1 number (see "VLANS") is inputed into my form. QUESTION: How do I make my dynamic form have a dynamic input box(which is created by checking the checkbox and calling the functionC1) inside it and still be able to pass the...
7
3575
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the same way like regular input fields can. strForm = "<form name=""myForm"" runat=""server"">" & vbCrLf strForm += "<asp:button name=""myName"" .... runat=""server"" />" & vbCrLf
4
3302
by: Venus | last post by:
Hello, Thanks for your reply. I understand that a control can be created dynamically in several ways: 1) using StringBuilder 2) using Controls.Add 3) using ASP PlaceHolder But this is just for the controls and not for the form itself. What I am trying to achieve is to create an entire form (including controls)
0
1486
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)): Can anyone make any suggestions on how to do it ? Thanks
9
1558
by: whisher | last post by:
Hi. I've managed this simple snippet: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Register</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script language="JavaScript" type="text/JavaScript"> var usernameError = 'Invalid String username';
18
24953
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin, that IE7 does not seem to offer any way to control the font size of a text input element.
0
2173
by: jianxin9 | last post by:
Hi everyone, I don't have a lot of experience with ASP and I was hoping someone could help me. I want to use our ASP form along with some javascript code to create a form where our patrons can select which department they will send the form to (we are trying to consolidate forms). This is what I have so far, but when I test the form, I keep getting this error message: Mailing Failed... Error is: FromAddress Property cannot be blank. You...
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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
8816
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
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...
1
8498
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7311
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
6162
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
4150
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
2726
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

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.