471,310 Members | 1,057 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

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").value
In ReadOnly this is just plain text, so I can use
getElementById("name").innerText.

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 1652
Hi,

document.getElementById("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").value
In ReadOnly this is just plain text, so I can use
getElementById("name").innerText.
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.getElementById(...);
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.selectedIndex].value ) {
alert(el[el.selectedIndex].value);
} else if ( el[el.selectedIndex].text ){
alert(el[el.selectedIndex].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").value In ReadOnly this is just plain ^^ ^^^^ see?
text, so I can use getElementById("name").innerText.

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 HTMLInputElement
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 (objectReference)
{
// input form control
if (typeof objectReference.tagName != "undefined"
&& objectReference.tagName.toLowerCase() == "input"
&& objectReference.type.toLowerCase() == "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").value In ReadOnly this is just plain
^^ ^^^^ see?
text, so I can use getElementById("name").innerText.

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 HTMLInputElement 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 "appropriate" 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 (objectReference)
<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.javascript 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 "appropriate" 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
I'm only replying here to not let this latest nonsense of yours go
uncommented while you made it impossible to clear this up calmly and
in private.

DU wrote:
Wow... I certainly missed the obvious.
That I am not responsible for the tag soup currently used for *redirection*?
Yes.
Your site, PointedEars, uses frames,
My home page is not a frameset of itself, it is alas wrapped into one I do
not have control over. (Which was also explained before ...) Subordered
sites like the UFPDB do use frames for valid reasons that I have already
pointed out before; this is not going to change for the foreseeable future.
(If you had cared to read ...) And one subordered site (Leisure Database)
uses frames because I did not know any better then (see below), which was
already going to change before your "comments".
has markup errors,
Occasionally, yes. I will not deny that. Sometimes pieces you did when
you did not know that much remain because you do not have the time to clear
them up, and do not want them to be inaccessible either, and sometimes one
has to find a balance between markup that is Valid and markup that works.
However, Valid markup should be the basis of all your work and so it is
strongly recommended to the uninitiated. Possibly some day you will find
out either truth for yourself.
I can't validate your CSS code using the CSS validation button on your own
page,
The problem is that the W3C CSS Validator currently cannot validate
stylesheets in certain resources delivered using server-side Content
Negotiatiation, which is a bug (the CSS Validator should send an
Accept-Language header as the Markup Validator does). But obviously
you have no clue what Content Negotiation is, else you would have
recognized "Not Acceptable" as a valid HTTP status code.

<http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.stud.tu-ilmenau.de%2F~thla-in%2Fscripts%2Ftest%2Fwhatami.en.php>
your site uses popups ads and a frame advertisement;
[...]
No, it does not. But you are lacking the competence to notice what
I already described in detail before.
Jesus!
You better go praying somewhere else than whining here.
This is my website, btw: [...]


Serving XHTML 1.0 Strict as text/html. YMMD!

Your unsolicited Web site advertisement is as desperately wanted here
as your falsified From header, in contrast to each and every Internet
standard, is. NOT.

Next troll, please.
PointedEars
Jul 23 '05 #11
Thomas 'PointedEars' Lahn wrote:
I'm only replying here to not let this latest nonsense of yours go
uncommented while you made it impossible to clear this up calmly and
in private.
Nonsense? We will see.
DU wrote:

Wow... I certainly missed the obvious.

That I am not responsible for the tag soup currently used for *redirection*?
Yes.


No, the more obvious that you missed. Look again, you will see it
(maybe) but probably not.
Your site, PointedEars, uses frames,

My home page is not a frameset of itself, it is alas wrapped into one I do
not have control over.


Yes you do, you just don't want to admit/realize that you do.
Don't put your site on a free site that uses frames. That is a choice
*you* made. For whatever reason, you made it. You control that decision.
(Which was also explained before ...) Subordered
sites like the UFPDB do use frames for valid reasons that I have already
pointed out before; this is not going to change for the foreseeable future.
Valid reason for Frames? Wow.

http://html-faq.com/htmlframes/?framesareevil
(If you had cared to read ...) And one subordered site (Leisure Database)
uses frames because I did not know any better then (see below), which was
already going to change before your "comments".
Talk is cheap. Especially after the fact. People who live in glass
houses shouldn't throw stones.
has markup errors,

Occasionally, yes. I will not deny that. Sometimes pieces you did when
you did not know that much remain because you do not have the time to clear
them up, and do not want them to be inaccessible either, and sometimes one
has to find a balance between markup that is Valid and markup that works.


And this is coming from a person who preaches to Validate Markup. I am
not sure about German usage of the English Language, but here we have a
word for that behavior - hypocrite.
However, Valid markup should be the basis of all your work and so it is
strongly recommended to the uninitiated.
So, you admit to being unitiated? Or, are you special? <gag me with a
fucking spoon>

ab***@comcast.net

Possibly some day you will find out either truth for yourself.
I want some of what ever you smoke, its good and has your brains fried.
I can't validate your CSS code using the CSS validation button on your own
page,

The problem is that the W3C CSS Validator currently cannot validate
stylesheets in certain resources delivered using server-side Content
Negotiatiation, which is a bug (the CSS Validator should send an
Accept-Language header as the Markup Validator does).


No, the problem is the link on your page points to the incorrect
resource that the validator can handle. It would only take a second or
so to change the link to point to the URL you provided. Or does that
just not register in your brain?

But obviously you have no clue what Content Negotiation is, else
you would have recognized "Not Acceptable" as a valid HTTP status code.
And you tell me that *I* made this personal? You are stupid.
<http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.stud.tu-ilmenau.de%2F~thla-in%2Fscripts%2Ftest%2Fwhatami.en.php>
Make your link point to that URL in your page, and then your CSS button
works. What is so freaking hard about that for you to comprehend?
your site uses popups ads and a frame advertisement;
[...]

No, it does not.


Yes, it does.

But you are lacking the competence to notice what I already described in detail before.
LOL, you really are as stupid as you act sometimes. The URL you provided
DOES use popups and frame advertisement. What is it about reality that
you have such a hard time with?

Jesus!

You better go praying somewhere else than whining here.


You just never learn.......
This is my website, btw: [...]

Serving XHTML 1.0 Strict as text/html. YMMD!

Your unsolicited Web site advertisement


Huh? What "advertisement"? You need to wake up from your dream world and
get back into reality.

is as desperately wanted here as your falsified From header,
in contrast to each and every Internet standard, is.

His From: header is not falsified. Learn the difference between an
invalid email address and a falsified header.

Patiently waiting your next rambling session - I need the laughs.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #12
DU
Thomas 'PointedEars' Lahn wrote:
I'm only replying here to not let this latest nonsense of yours go
uncommented while you made it impossible to clear this up calmly and
in private.

PointedEars, you should wake up and start cleaning your own website
before preaching to others the kind of valid code you don't even serve
your own webpages. You're either an incompetent or a liar.
DU wrote:

Wow... I certainly missed the obvious.

That I am not responsible for the tag soup currently used for *redirection*?
Yes.

Your site, PointedEars, uses frames,

My home page is not a frameset of itself, it is alas wrapped into one I do
not have control over. (Which was also explained before ...) Subordered
sites like the UFPDB do use frames for valid reasons that I have already
pointed out before; this is not going to change for the foreseeable future.
(If you had cared to read ...) And one subordered site (Leisure Database)
uses frames because I did not know any better then (see below), which was
already going to change before your "comments".


You chose all that; you can't say you never chose that site. The obvious
problem is that you are extremely intolerant with people you reply to in
this newsgroup when your very own website should have moderated your temper.
has markup errors,

Occasionally, yes. I will not deny that. Sometimes pieces you did when
you did not know that much remain because you do not have the time to clear
them up, and do not want them to be inaccessible either, and sometimes one
has to find a balance between markup that is Valid and markup that works.
However, Valid markup should be the basis of all your work and so it is
strongly recommended to the uninitiated. Possibly some day you will find
out either truth for yourself.


Even your own examples given in your webpages have markup errors and the
kind of code you have denounced here before. You're definately not
consequent. You are blasting often people in this newsgroup for errors
that you do yourself in your own webpages and in your own examples.
I can't validate your CSS code using the CSS validation button on your own
page,

The problem is that the W3C CSS Validator currently cannot validate
stylesheets in certain resources delivered using server-side Content
Negotiatiation, which is a bug (the CSS Validator should send an
Accept-Language header as the Markup Validator does). But obviously
you have no clue what Content Negotiation is, else you would have
recognized "Not Acceptable" as a valid HTTP status code.

<http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.stud.tu-ilmenau.de%2F~thla-in%2Fscripts%2Ftest%2Fwhatami.en.php>


*You* put a CSS validation button on your webpages. *You* put clickable
images that you know will not work. *You* create failures in your
website. *You* confuse your users, your visitors. *You* are the only one
to blame for doing all that.
your site uses popups ads and a frame advertisement;
[...]

No, it does not. But you are lacking the competence to notice what
I already described in detail before.


Your site uses popups ads and a frame advertisement. Anyone reading this
post can verify for himself and decide who is telling the truth here.
Jesus!

You better go praying somewhere else than whining here.

This is my website, btw: [...]

Serving XHTML 1.0 Strict as text/html. YMMD!


I serve several pages as text/html: that's true. The most recent ones I
added are HTML 4.01 strict and not XHTML 1.0 strict served as text/html.
Nevertheless, all webpages at my website pass validation: it's not your
case, not your case at all.

PointedEars, you are a liar or you're an hypocrite.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Ksenia Marasanova | last post: by
2 posts views Thread by Dave Griffiths | last post: by
1 post views Thread by Koral | last post: by
reply views Thread by Alexnb | last post: by
reply views Thread by rosydwin | last post: by

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.