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

Read Value

How can I determine the value TEST from both of the scenarios below?
For the <P> tag I can use OMON_3.innerText, but with the <Input> tag I
need to use OMON_3.value. However, in my application depending on how
the page is viewed, either the <P> tag will be displayed or <Input>.
Is there a way to find the value in both cases with the same piece of
code?

<P id="OMON_3">TEST</P>

<INPUT style="width:135px" maxlength="15" name="OMON_3" tabindex="4"
value="TEST" />

Jul 23 '05 #1
3 1364
wrote on 03 mrt 2005 in comp.lang.javascript:
How can I determine the value TEST from both of the scenarios below?
For the <P> tag I can use OMON_3.innerText, but with the <Input> tag I
need to use OMON_3.value. However, in my application depending on how
the page is viewed, either the <P> tag will be displayed or <Input>.
Is there a way to find the value in both cases with the same piece of
code?

<P id="OMON_3">TEST</P>

<INPUT style="width:135px" maxlength="15" name="OMON_3" tabindex="4"
value="TEST" />


Why not use an id in both instances, like:

<INPUT name="OMON_3" id="OMON_3" value="TEST">

=======================

but to have it your way:

<script type='text/javascript'>
if (document.getElementById('OMON_3').value)
alert(document.getElementById('OMON_3').value)
else
alert(document.getElementsByName('OMON_3')[0].innerHTML);
</script>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
la*******@gmail.com wrote:
For the <P> tag I can use OMON_3.innerText,
If IE is the only browser to be exposed to this script, then yes you can.
However, in my application depending on how the page is viewed,
either the <P> tag will be displayed or <Input>. Is there a way to
find the value in both cases with the same piece of code?
Once you have a reference to an element, you can determine the type of
element using either the tagName (for HTML documents) or nodeName (for
either XML-based or HTML documents) properties.

However, you're going to have difficultly getting that reference at
all with the mark-up you've posted: the P element can be obtained
using the getElementById method, however the INPUT element doesn't
have an id so it can't[1]. Generate an id attribute for both elements
and you can use something like:

var e = document.getElementById('OMON_3'),
v = '';

if(e) {
if(('P' == e.tagName) && e.firstChild) {
v = e.firstChild.data;
} else if('INPUT' == e.tagName) {
v = e.value;
}
}

[snip]
<INPUT style="width:135px" maxlength="15" name="OMON_3"
tabindex="4" value="TEST" />


For an XML-based document, the strings used to compare tag names will
need to be lowercase, and the property changed from tagName to
nodeName. That said, despite the XHTML-like syntax used above, you
clearly aren't using XHTML as HTML-adopted tag names must be
lowercase. Remove the forward slash (/) from the end of the tag.

Hope that helps,
Mike
[1] The behaviour exhibited by IE where getElementById will retrieve
an element reference by either id or name is erroneous and it
should not be relied upon. Whichever Microsoft decision maker
thought that one up is an idiot!

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Perfect! I know the structure seems a little odd, but it is what I
have to work with. Thank you for the help!

Jul 23 '05 #4

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...
3
by: jason | last post by:
i am using an XMLValidatingReader in a manner similar to the following code example: string sReturn = ""; XmlValidatingReader oXML = new XmlValidatingReader(sXMLString, XmlNodeType.Document,...
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
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...
2
by: dumbo | last post by:
Hello, who gives one more shining solution to me? My application load form "A". The form "A" by means of a push-button load form "B" that in its turn by means of a push-button load form "C". Form...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
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...
4
by: Pim75 | last post by:
Hello, I have to read a XML file in ASP and save the values in a database. I can get this work, but I cannot read some nested nodes of the xml file. This is a part of the XML file: ...
0
by: martinmercy2001 | last post by:
Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.