Prioritize form field

F

Frowning Freezer

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[n];
}
for (i=0; !x && d.layers && i < d.layers.length; i++) {
x = getele(n, d.layers.document);
}
if (!x && d.getElementById) {
x = d.getElementById(n);
}
return x;
}


Regards,
Allan Jensen
 
T

Thomas 'PointedEars' Lahn

Safalra said:
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[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 ...
for (i=0; !x && i < d.forms.length; i++) {
x = d.forms[n];
}

This is, however, the wrong solution [...]


.... ACK, in double meaning.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top