473,785 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deriving Element Names from associated Labels

All,

I am hoping someone might be able to put me out of my misery and let me
know if it is possible or not to dervie the name of an element in a
form, based on its associated label, only knowing the value of the
label, and working on the assumption that the <label for="???" links to
the <select id="???"

Below is the html that I am working from, which has been dynamically
created. Unfortunately I have no control over this at all. I need to
obtain the name of the drop down, so I can derive the country code
(.value) and the country name (.text) but the only constant I have is
knowing that it will be labelled up "Country".

Below is the HTML I am seeing along with the Pseduo code I think I will
need to implement. I have
removed my JavaScript effort to save embarressment on my part.

If anyone can help, provide code, or point me in the right direction
then I would really appreciate it.

Thanks All,
Martin

<html>
<head>
<script>
function abc(form_name)
{
// Pseudo Code

Search form to find the Label "Country"
Derived 'For' value. i.e. <label for = "N50"

Re-search form to find an id matching the previously found for value
i.e. search form where id for form element= "N50" <select id="N50"
Return name of form element i.e. HrAddressFlex0
}
</script>
</head>
<body>
<form id="DefaultForm Name" name="DefaultFo rmName" method="POST"
action="www.dum myurl.com">
<table>
<tr>
<td align="right" nowrap>
<label for="N50">Count ry</label>
</td>
<td nowrap>
<select id="N50" name="HrAddress Flex0">
<option value="AFG">Afg hanistan</option>
<option value="ALB">Alb ania</option>
<option value="ALG">Alg eria</option>
</select>
</td>
</tr>
<tr>
<td align="right" nowrap></td><td>
<input type="button" name="submit_bu tton" value="submit"
onclick="abc(th is.form)">
</td>
<tr>
</table>
</form>
</body>
</html>
Jul 20 '05 #1
2 3479
Martin O'Rourke <Ma************ @oracle.com> writes:
I am hoping someone might be able to put me out of my misery and let me
know if it is possible or not to dervie the name of an element in a
form, based on its associated label, only knowing the value of the
label, and working on the assumption that the <label for="???" links to
the <select id="???" .... the only constant I have is knowing that it will be labelled up
"Country".


Are you sure even the HrAddressFlex0 is unguessable? Even in part ...
if you know it's always "??AddressFlex< number>", or something else that
is sufficiently unique, then it is easier going for the select element
directly.

Anyway:
---
<script type="text/javascript">
function findCountry() {
var labels = document.getEle mentsByTagName( "label");
for (var i=0;i<labels.le ngth;i++) {
if (labels[i].firstChild.nod eValue.match(/^\s*Country\s*$/)) {
return document.getEle mentById(labels[i].htmlFor);
}
}
}
</script>
---

Tested in IE6, Mozilla and Opera 7. If you want to adapt it for
earlier IE's, you will want to add extra cases for when
"getElementsByT agName", "getElementById " and "firstChild " doesn't
exist.

In IE4, these can be emulated by "document.all.t ags['label']",
"document.a ll[id]" and ".innerText ", but it will double the size of
the functio.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2


Martin O'Rourke wrote:
All,

I am hoping someone might be able to put me out of my misery and let me
know if it is possible or not to dervie the name of an element in a
form, based on its associated label, only knowing the value of the
label, and working on the assumption that the <label for="???" links to
the <select id="???"

Below is the html that I am working from, which has been dynamically
created. Unfortunately I have no control over this at all. I need to
obtain the name of the drop down, so I can derive the country code
(.value) and the country name (.text) but the only constant I have is
knowing that it will be labelled up "Country".

Below is the HTML I am seeing along with the Pseduo code I think I will
need to implement. I have
removed my JavaScript effort to save embarressment on my part.

If anyone can help, provide code, or point me in the right direction
then I would really appreciate it.


Here is a solution that searches the document for a <label> element with
content Country, then looks for the appropriate select element and
outputs its value/text

<html>
<head>
<script>
function getCountry (labelContent) {
var labels;
if (document.all) {
labels = document.all.ta gs('label');
}
else if (document.getEl ementsByTagName ) {
labels = document.getEle mentsByTagName( 'label');
}
if (labels) {
var labelText, label;
for (var i = 0; i < labels.length; i++) {
label = labels[i];
labelText = getInnerText(la bel);
if (labelText == labelContent) {
var selectId = label.htmlFor;
var select;
if (document.all) {
select = document.all[selectId];
}
else if (document.getEl ementById) {
select = document.getEle mentById(select Id);
}
if (select) {
var value = select.options[select.selected Index].value;
var text = select.options[select.selected Index].text;
alert('value: ' + value + '; text: ' + text);
}
break;
}
}
}
}

function getInnerText (element) {
if (typeof element.innerTe xt != 'undefined') {
return element.innerTe xt;
}
else if (document.creat eRange) {
var range = document.create Range();
range.selectNod eContents(eleme nt);
return range.toString( );
}
}
</script>
</head>
<body>
<form id="DefaultForm Name" name="DefaultFo rmName" method="POST"
action="www.dum myurl.com">
<table>
<tr>
<td align="right" nowrap>
<label for="N50">Count ry</label>
</td>
<td nowrap>
<select id="N50" name="HrAddress Flex0">
<option value="AFG">Afg hanistan</option>
<option value="ALB">Alb ania</option>
<option value="ALG">Alg eria</option>
</select>
</td>
</tr>
<tr>
<td align="right" nowrap></td><td>
<input type="button" name="submit_bu tton" value="submit"
onclick="getCou ntry('Country') ">
</td>
<tr>
</table>
</form>
</body>
</html>

Works with IE4+, Netscape 6+ and at least Opera 7

--

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

Jul 20 '05 #3

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

Similar topics

28
4111
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come across any examples of a construct like what I show below. Perhapes it's simply a bad idea. If there is something fundamentally wrong with this approach please let me know. Can anybody tell me if there is a way to get the following to work? I...
2
6801
by: pintihar | last post by:
I am trying to map external xml documents to a class in dotnet. The problem is that the elements of the input xml will have different names than the properties of the class. How do I create the xslt that will allow me to transform the inbound xml into another xml resource with the elements renamed so that I can serialize it to an instance of the class. I assume that this is best done with xslt.transform, but I am not sure how to write...
5
1670
by: bclark76 | last post by:
I am getting a strange error, maybe someone knows why it is occurring.. I get the following error when I try to validate Untitled8.xml in Altova XMLSPY: Validation error in another file: Untitled3.xsd The message in XMLSPY in Untitled3.xsd is: The file is not valid: The content model of complex type { no name } is not a valid restriction of the content model of complex type 'Condiment_Type'
8
2500
by: Russ | last post by:
Does it ever make sense to derive a class from a basic type such as float or int? Suppose, for example, that I want to create a class for physical scalars with units. I thought about deriving from float, then adding the units. I played around with it a bit, but it doesn't seem to work very well. Am I missing something here? Thanks.
3
1433
by: libsfan01 | last post by:
Hi all whats wrong with this script? i have named a form text box dynamically using php each row has a textbox called q101, q102 ... etc and i want to make sure my users dont input a quantity greater than the value of that box. to know which box applies to which of the products i have used a radio button which contains the id of the product which corresponds to the name of the box when a q is added to the front (javascript seems not to...
1
9121
by: UncleRic | last post by:
Environment: Firefox 2.0. What is the XPath Syntax for accessing the NAMES (vs VALUES) of an element's attributes? ...or more accurately, Names & their associated values.
7
3511
by: mcha226 | last post by:
Hi All I have to build a page which includes a select element (as a drop down menu) with all the country names in it. However I just found out that the need to be repeated many times so the client can select as many country as they can in their around the world trip planning. I don't want to repeat this long list multiple times, because I want to save bandwidth. Is there a more efficient way to do this rather than using JavaScript to...
1
3501
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good idea? I was under the impression that deriving from std classes that do not have virtual methods was bad. Is ostream designed to be derived from? If, so are there any resources on how to do this properly? ostream has alot if internals that look...
6
3730
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML files into a tab-separated text file that I want to import into the database. I have run into the following problem: in some documents there are missing elements, for instance the volume and issue number of an article is not there (i.e. it is defined...
0
9645
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
10148
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
10091
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
9950
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
8972
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.