Prioritize form field

Discussion in 'Javascript' started by Frowning Freezer, Aug 28, 2008.

  1. 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
     
    Frowning Freezer, Aug 28, 2008
    #1
    1. Advertising

  2. Safalra (Stephen Morley) wrote:
    > On Thu, 28 Aug 2008 16:41:35 +0200, Frowning Freezer wrote:
    >> 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
    --
    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
     
    Thomas 'PointedEars' Lahn, Aug 31, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willem Oosthuizen

    Coding style to prioritize certain inputs

    Willem Oosthuizen, Sep 2, 2003, in forum: VHDL
    Replies:
    5
    Views:
    500
    Mike Treseler
    Sep 4, 2003
  2. NotGiven
    Replies:
    3
    Views:
    354
    Michael D. Kersey
    May 13, 2004
  3. GavMc
    Replies:
    4
    Views:
    341
    Evertjan.
    Sep 22, 2005
  4. NotGiven
    Replies:
    3
    Views:
    307
    Michael D. Kersey
    May 13, 2004
  5. Sound
    Replies:
    2
    Views:
    450
    Randy Webb
    Sep 28, 2006
Loading...

Share This Page