473,545 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting contents from field or tag

Folks,

I need to get the contents of a form attribute.

In Read/Write mode this is field, so I can use getElementById( "name").val ue
In ReadOnly this is just plain text, so I can use
getElementById( "name").innerTe xt.

I need to do it dynamically though, so is there anyway I can look at the
object returned by getElementById( ) and determine if it was a field or a
tag?

(I can then call ".value" or ".innerText " as appropriate)
TIA -Adam


Jul 23 '05 #1
12 1770
Hi,

document.getEle mentById("name" ).tagName

it return the name of the tag

cheers!

Jeje

Jul 23 '05 #2
Adam Lipscombe wrote:
Folks,

I need to get the contents of a form attribute.

In Read/Write mode this is field, so I can use getElementById( "name").val ue
In ReadOnly this is just plain text, so I can use
getElementById( "name").innerTe xt.
You need to provide a small HTML example. A readonly form element can
still be given a value that is displayed in the text box, but the user
can't change it directly, e.g.

<input type="text" value="I'm readonly" readonly
onclick="alert( this.value);">

I need to do it dynamically though, so is there anyway I can look at the
object returned by getElementById( ) and determine if it was a field or a
tag?
getElementById( ) will return a reference to an element, it is never a
'field' or 'tag'.

Once you have a reference, you can access the attributes or properties
of the element. If you are unsure whether the element you get hold of
has a value attribute, then test it first. You can also check the type
of element you've found by looking at the nodeName:

var el = document.getEle mentById(...);
if ( 'INPUT' == el.nodeName ) {
// do input element processing...
}

You should also remember that some form elements like options do not
necessarily have a value, e.g.

<option>A</option>

The value of the above option element in W3C compliant browsers is the
text 'A', but in IE the value will be undefined, you have to get the
text attribute of the option explicitly.

Browsers like Firefox will be happy with:

if ( el.value ) {
// do something with the value
} else if ( el.text) {
// do something with the text
} else {
// What now, both value and text are empty...
}

But not IE. You must try a little harder and use the selectedIndex
property of the select:

<input type="text" value="I'm readonly" readonly
onclick="blurt( this)">
<select onchange="blurt (this);">
<option selected></option>
<option>opt 1</option>
<option value="Value of option 2">opt 2</option>
<option>opt 3</option>
</select>

<script type="text/javascript">
function blurt(el) {
if ('SELECT' == el.nodeName ){
if ( el[el.selectedInde x].value ) {
alert(el[el.selectedInde x].value);
} else if ( el[el.selectedInde x].text ){
alert(el[el.selectedInde x].text);
} else {
alert('Value and text are empty...');
}
} else if ( 'INPUT' == el.nodeName ) {
if ( 'text' == el.type ){
alert( el.value );
}
// Add cases for radio, checkbox, etc...
}
}
</script>

You may wish to use a switch block to handle the various cases rather
than a bunch of ifs.

(I can then call ".value" or ".innerText " as appropriate)


There is no W3C equivalent for Microsoft's innerText (which is not well
supported outside IE), however depending on your particular
circumstance, it may be simple to provide similar functionality.
--
Rob
Jul 23 '05 #3
Adam Lipscombe wrote:
I need to get the contents of a form attribute.
There is no such thing.
In Read/Write mode this is field,
Probably you mean `form control'.
so I can use getElementById( "name").val ue In ReadOnly this is just plain ^^ ^^^^ see?
text, so I can use getElementById( "name").innerTe xt.

I need to do it dynamically though, so is there anyway I can look at the
object returned by getElementById( ) and determine if it was a field or a
tag?
Your wording shows your cluelessness. Probably you mean that you want
to find out what type of DOM object was returned, an HTMLInputElemen t
object if type "text" or another DOM object.
(I can then call ".value" or ".innerText " as appropriate)


`.innerText' is not appropriate, it is proprietary, while getElementById( )
is part of a Web standard. You should avoid mixing both without tests.
See <http://pointedears.de/scripts/test/whatami>, §2.

Non-function properties are not called (as methods are), they are accessed.

Maybe you are looking for this:

// don't use document.get... By...() if you don't have to
var objectReference = document.forms[...].elements["name"];

if (objectReferenc e)
{
// input form control
if (typeof objectReference .tagName != "undefined"
&& objectReference .tagName.toLowe rCase() == "input"
&& objectReference .type.toLowerCa se() == "text")
{
... objectReference .value ...
}

// no form control, W3C DOM Level 3
else if (typeof objectReference .textContent != "undefined" )
{
... objectReference .textContent ...
}

// no form control; IE proprietary
else if (typeof objectReference .innerText != "undefined" )
{
... objectReference .innerText ...
}
HTH

PointedEars
Jul 23 '05 #4
Thomas 'PointedEars' Lahn wrote:
Adam Lipscombe wrote:

I need to get the contents of a form attribute.

There is no such thing.


So, forms don't have attributes? What *do* they have? I have always been
led to believe that the action *attribute* of a form element was just
that - an attribute. Yet now you say there is no such thing? Please
educate me.
In Read/Write mode this is field,

Probably you mean `form control'.


As someone whose native language is not English, you should at least
understand the short comings of peoples grasp (or lack thereof) the
differences in the way people describe things.
so I can use getElementById( "name").val ue In ReadOnly this is just plain
^^ ^^^^ see?
text, so I can use getElementById( "name").innerTe xt.

I need to do it dynamically though, so is there anyway I can look at the
object returned by getElementById( ) and determine if it was a field or a
tag?

Your wording shows your cluelessness.


Yours doesn't show only cluelessness, it shows wreckless stupidity by
attempting to talk down to a poster when its not needed. Practice what
you preach Thomas.
Probably you mean that you want to find out what type of DOM object
was returned, an HTMLInputElemen t object if type "text" or another DOM object.
You are a master of the obvious, no?


(I can then call ".value" or ".innerText " as appropriate)

`.innerText' is not appropriate, it is proprietary, while getElementById( )
is part of a Web standard.


Whether innerText is "appropriat e" or not is totally and 100% directly
related to its context of use.

And what "Web Standard" standardized getElementByID? Since you are
nitpicking the wording, I will too. Its not a "Web Standard", its an
ECMA standard. The Web itself has no "Standard", it has standards
writing bodies that attempt to write standards for the Web.
You should avoid mixing both without tests.
See <http://pointedears.de/scripts/test/whatami>, §2.
For a site, such as that one, to be taken seriously, you should at least
invest some of that §2 in a decent web server setup so that you do not
have unprofessional popups and ads all over it.
Non-function properties are not called (as methods are), they are accessed.

Maybe you are looking for this:

// don't use document.get... By...() if you don't have to
var objectReference = document.forms[...].elements["name"];
And if the form or element doesn't exist? Or, it exists outside a form?

if (document.forms && document.forms[...] &&
document.forms[...].elements["name"]){
var objectReference = document......
}
else{
if (objectReferenc e)
<snip>
}


}

If you are going to rant about minor issues such as what people call
things, and then post code to "correct" them and include code to do
object/method detection, you could at least apply the same nit-picking
to your own code.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #5
Randy Webb wrote:
Thomas 'PointedEars' Lahn wrote:
Adam Lipscombe wrote:
I need to get the contents of a form attribute. There is no such thing.


So, forms don't have attributes?


They have attributes in the natural language sense, including width, height
color and so on. They do not have attributes in the markup language sense,
this applies for _`form' elements_. The difference is important as it does
not need a `form' element to render a form.
[...]
In Read/Write mode this is field, Probably you mean `form control'.


As someone whose native language is not English, you should at least
understand the short comings of peoples grasp (or lack thereof) the
differences in the way people describe things.


Bad wording often indicates cluelessness that is tried to be covered by
buzzwords picked up somewhere and used unreflected. It certainly does
in this case (example: `determine if it was a field or a tag'), and so
it was commented on it. Reasonable readers would think about that and
get themselves informed in the process.
(I can then call ".value" or ".innerText " as appropriate)

`.innerText' is not appropriate, it is proprietary, while
getElementById( ) is part of a Web standard.


Whether innerText is "appropriat e" or not is totally and 100% directly
related to its context of use.

And what "Web Standard" standardized getElementByID?


W3C DOM Level 1 and W3C DOM Level 2 do.
Since you are nitpicking the wording, I will too. Its not a "Web
Standard", its an ECMA standard.
1. ECMAScript (ECMA-262) is an ECMA standard which was developed from
JavaScript and JScript which were intended primarily for Web use, both
client-side and server-side. It can be included in the list of Web
standards that exist nowadays.

2. It's about the DOM, not the programming language, a difference that you
still are either not willing or unable to comprehend although it has been
explained to you several times.
The Web itself has no "Standard",
Yes, it has _standards_. DOM Level 1 and 2 are two of them.

I, too, thought before that W3C specifications are not standards,
however the W3C (now?) makes it quite clear that their specifications/
Recommendations can be considered Web standards since they are created
using a standardization process:

<http://www.w3.org/Consortium/>

This is also how their work is percepted among many other participants
than you.
it has standards writing bodies that attempt to write standards for
the Web.
They don't attempt to do it, they just do it. Note that these bodies have
members that are other organizations involved in building and maintaining
the Web. And the W3C, as one of the bodies mentioned, requires their
standards to have, among other conditions, at least two interoperable
implementations before it becomes a Recommendation (that is why, e.g., CSS
2.1 is after more than a year still only a Candidate Recommendation) , so
it is not all just theory as you appear to suggest.
You should avoid mixing both without tests.
See <http://pointedears.de/scripts/test/whatami>, §2.


For a site, such as that one, to be taken seriously, you should at least
invest some of that §2 in a decent web server setup so that you do not
have unprofessional popups and ads all over it.


Yes, now you have to get personal, for you have from little to no valid
arguments. Some of the dirt you are throwing at me is somehow going to
stay on my clothes, yes? No, in fact you are throwing dirt at yourself
instead. I'd pity you if I had time for it.

But JFYI, as you are this interested in my well-being: Plans to move to
another server which would no longer require frames domain redirection
and so no more mandatory ads (that I don't like either), and yet be non-
expensive (actually, it would be EUR 0 plus remote root access :)) are
already underway and should be completed within the next month. It was
planned to be completed by now but both timing and legal issues have
forced me to postpone it by another three weeks. I am still posting the
domain-related URIs since the URIs redirected to will become inaccessible
in the future as I will not have means for automated redirection for them
(the account will just be canceled in time by the responsible admin).
However, alas I am not so sure that you manage to understand that.
Non-function properties are not called (as methods are), they are
accessed.

Maybe you are looking for this:

// don't use document.get... By...() if you don't have to
var objectReference = document.forms[...].elements["name"];


And if the form or element doesn't exist?


If the referenced object does not exist, the code will fail. It has
intentionally been left as an exercise to the reader to complete the
feature test using the example given. This group is intended as a
discussion medium, not as a source for complete solutions.
Or, it exists outside a form?
That is a valid possibility, yet not a likely one. The OP would have to
use proprietary objects and DOM Level 2 methods then. I recommend against
that; making the element a descendant element of a `form' element is less
painful as the resulting DOM subtree can be accessed by both standardized
and downwards compatible references.
[...]
If you are going to rant about minor issues such as what people call
things, and then post code to "correct" them and include code to do
object/method detection, you could at least apply the same nit-picking
to your own code.


See above. And please, get a life.
PointedEars
Jul 23 '05 #6
RobG wrote:
Adam Lipscombe wrote:
(I can then call ".value" or ".innerText " as appropriate)


There is no W3C equivalent for Microsoft's innerText [...]


Not true. The `textContent' attribute of the Node interface of W3C DOM
Level 3 Core, implemented in newer Gecko-based user agents, meets the
need for that:

<http://www.mozilla.org/docs/dom/reference/levels.html>

And your sender address is still forged. (But not always, so forging
does not make sense in that regard, too.)
PointedEars
Jul 23 '05 #7
DU
Thomas 'PointedEars' Lahn wrote:
Randy Webb wrote:

Thomas 'PointedEars' Lahn wrote:
Adam Lipscombe wrote:

I need to get the contents of a form attribute.

There is no such thing.
So, forms don't have attributes?

They have attributes in the natural language sense, including width, height
color and so on. They do not have attributes in the markup language sense,
this applies for _`form' elements_.


You are usually very word-sensitive; this post of yours is no exception.

People will not relate well to what you say when they feel put down by
post like yours; authoritative ones. They will better relate to posts
that explain, that justify and document or lead to resources. It's more
of an attitude that addresses the intelligence of readers and address
their will to cooperate in improving the web rather than an attitude
that lectures, that admonishes, that scolds.
If you ever want to write a W3C quality tip for webmaster, this is what
W3C demands: a proper tone when addressing people and an effort to
unverbiage specs.

[snipped]


See above. And please, get a life.
PointedEars


Quite typical of you.

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Firefox 1.0.4 :)
Jul 23 '05 #8
DU
Thomas 'PointedEars' Lahn wrote:
RobG wrote:

Adam Lipscombe wrote:
(I can then call ".value" or ".innerText " as appropriate)


There is no W3C equivalent for Microsoft's innerText [...]

Not true. The `textContent' attribute of the Node interface of W3C DOM
Level 3 Core,


Strangely enough, you once said in this newsgroup there was no W3C
equivalent to innerText. Then I told you about textContent.

Anyway, can you change your tone a bit when you answer post? :)

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Firefox 1.0.4 :)
Jul 23 '05 #9
DU
Thomas 'PointedEars' Lahn wrote:
Randy Webb wrote:

Thomas 'PointedEars' Lahn wrote:

[snipped]
See <http://pointedears.de/scripts/test/whatami>, §2.


For a site, such as that one, to be taken seriously, you should at least
invest some of that §2 in a decent web server setup so that you do not
have unprofessional popups and ads all over it.

Yes, now you have to get personal, for you have from little to no valid
arguments. Some of the dirt you are throwing at me is somehow going to
stay on my clothes, yes? No, in fact you are throwing dirt at yourself
instead. I'd pity you if I had time for it.


Wow... I certainly missed the obvious. Your site, PointedEars, uses
frames, has markup errors, I can't validate your CSS code using the CSS
validation button on your own page, your site uses popups ads and a
frame advertisement; you're even using here and there the very same code
in your webpage examples that you regularly reports as unneeded or as
improper or as invalid in others' post. Jesus! You should start applying
to your website what you preach to others. You should have a cold,
honest look at your own website and then try to fix your website
problems before tackling others' sites.
This is basic common sense. This is what DrPhil and Oprah Winfrey would
tell you here.

This is my website, btw:
http://www.gtalbot.org/
and you'll find a icon button for responsible advertisement at the
bottom of these pages
http://www.gtalbot.org/Netscape7/Netscape7Section.html
and
http://www.gtalbot.org/Netscape7/Pop...Netscape7.html

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Firefox 1.0.4 :)
Jul 23 '05 #10

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

Similar topics

9
3688
by: Ksenia Marasanova | last post by:
Hi, I have a little problem with encoding. Was hoping maybe anyone can help me to solve it. There is some amount of data in a database (PG) that must be inserted into Excel sheet and emailed. Nothing special, everything works. Except that non-ascii characters are not displayed properly. The data is stored as XML into a text field. When I...
2
2273
by: Dave Griffiths | last post by:
Access 97 - I have a form with a single unbound text field. I want to have a timer event which periodically saves the contents of that field. But I noticed that if the focus stays on the field then then the text doesn't get saved. It's like the text isn't there until focus is moved off the field. So does anyone know how to grab the contents...
3
1312
by: John M | last post by:
I'm lost on somethingthat I feel ought to be simple - getting the contents of a field! The database relates to school students and their subjects. The current record has as its source a query which combines a number of tables, each for a specific subject. These have a number of common fields such as 'target'. I want to fnd say the total...
6
1339
by: Br | last post by:
Hi everyone, Another newbie-type question, Here's what I'd like to do: 1) Have a user type something into an input-field 2) Transfer that information into a database 3) Extract that information from the database AT WILL 4) Display it on a screen.
7
2000
by: MN | last post by:
I am using a program called IDWorks which is reading and writing to an MS Access format database. The IDWorks program can't adjust the formatting on a displayed field in a way that we need so I need a way to duplicate a field in Access so that it will have the exact copy of the other field. Is there some way to have a field definition set up...
2
6099
by: shadowman | last post by:
So here's the situation: I need to write a PHP script which accepts form submissions using all methods (GET and POST) and all content types (application/x-www-form-url-encoded and multipart/form-data). And for POST method, it has to be able to get the exact bit-for-bit accurate contents of the POST body. Now here's the problem: I'm stuck with...
1
1376
by: Koral | last post by:
Hi, I have a SELECT FROM TABLE query and in that table there is a field of type image. Result of this select goes throug internet do its destination. But in fact I need only to know if in this field is or not an image. Is there any funciotn which gives me information about contents of image fields? Regards, Paul
2
2892
by: hotflash | last post by:
Hi Master CroCrew, I found a good PURE ASP that will allow you to upload 10MB file to the server and the file contents such as Network, Author, Title, etc... will insert to MS Access at the same time. Below is a working script that I used. Let's say after the file is uploaded to the server and a record created with the file contents above...
0
166
by: Alexnb | last post by:
The trick to this one is that the html looks something like this: <td width="100%" colspan="2"> american, /browse/blue blue , /browse/brick brick , brie, cheddar, cheshire, /browse/churn churn ,
0
7465
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...
0
7805
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
7416
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
7752
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...
0
5969
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...
0
4944
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...
0
3449
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...
1
1878
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
1
1013
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.