Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 03:45 PM
Frowning Freezer
Guest
 
Posts: n/a
Default Prioritize form field

How can the function below be rewritten to prioritize form fields instead of
other objects with the same name?

For example I want getele("title") to retrieve the form field named "title"
instead of document.title - how do I get it to do that?

function getele(n, d){
//argument 'n' is the name of the object you want to get
//argument 'd' is the document object, it is not required
var p,i,x;
if (!d) {
d = document;
}
if ( (p = n.indexOf("?")) 0 && parent.frames.length) {
d = parent.frames[n.substring(p+1)].document;
n = n.substring(0,p);
}
if ( !(x=d[n]) && d.all) {
x = d.all[n];
}
for (i=0; !x && i < d.forms.length; i++) {
x = d.forms[i][n];
}
for (i=0; !x && d.layers && i < d.layers.length; i++) {
x = getele(n, d.layers[i].document);
}
if (!x && d.getElementById) {
x = d.getElementById(n);
}
return x;
}


Regards,
Allan Jensen


  #2  
Old August 30th, 2008, 02:25 PM
Safalra (Stephen Morley)
Guest
 
Posts: n/a
Default Re: Prioritize form field

On Thu, 28 Aug 2008 16:41:35 +0200, Frowning Freezer wrote:
Quote:
How can the function below be rewritten to prioritize form fields instead of
other objects with the same name?
>
For example I want getele("title") to retrieve the form field named "title"
instead of document.title - how do I get it to do that?
>
function getele(n, d){
//argument 'n' is the name of the object you want to get
//argument 'd' is the document object, it is not required
var p,i,x;
if (!d) {
d = document;
}
if ( (p = n.indexOf("?")) 0 && parent.frames.length) {
d = parent.frames[n.substring(p+1)].document;
n = n.substring(0,p);
}
if ( !(x=d[n]) && d.all) {
x = d.all[n];
}
for (i=0; !x && i < d.forms.length; i++) {
x = d.forms[i][n];
}
for (i=0; !x && d.layers && i < d.layers.length; i++) {
x = getele(n, d.layers[i].document);
}
if (!x && d.getElementById) {
x = d.getElementById(n);
}
return x;
}

You can move the following if-block up in the function:

for (i=0; !x && i < d.forms.length; i++) {
x = d.forms[i][n];
}

This is, however, the wrong solution - rather than using this general
'getele' function which tries to find something somewhere in the document
with the appropriate element type/name attribute/id attribute, it would be
better to search only within the specific namespace required (form field
name, in this case).

--
Safalra (Stephen Morley)

A Colour Picker Widget For javascript:
http://www.safalra.com/web-design/ja...colour-picker/
  #3  
Old August 31st, 2008, 10:45 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Prioritize form field

Safalra (Stephen Morley) wrote:
Quote:
On Thu, 28 Aug 2008 16:41:35 +0200, Frowning Freezer wrote:
Quote:
>How can the function below be rewritten to prioritize form fields instead of
>other objects with the same name?
>>
>For example I want getele("title") to retrieve the form field named "title"
>instead of document.title - how do I get it to do that?
>>
>function getele(n, d){
> //argument 'n' is the name of the object you want to get
> //argument 'd' is the document object, it is not required
> var p,i,x;
> [...]
> for (i=0; !x && i < d.forms.length; i++) {
> x = d.forms[i][n];
> }
> [...]
> if (!x && d.getElementById) {
> x = d.getElementById(n);
> }
> return x;
>}
>
>
You can move the following if-block up in the function:
Then the priority of it as compared to now would be *lower*, because
execution does not return after the assignment. So the *last* assignment
wins. So it should be moved down instead, but ...
Quote:
for (i=0; !x && i < d.forms.length; i++) {
x = d.forms[i][n];
}
>
This is, however, the wrong solution [...]
.... ACK, in double meaning.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles