document.all[txtObj] works but document.getElementById[txtObj] fails

Discussion in 'Javascript' started by wk, Apr 22, 2005.

  1. wk

    wk Guest

    hi,
    in my code
    document.all[txtObj].value works but
    document.getElementById[txtObj].value fails.


    any ideas why?
    wk, Apr 22, 2005
    #1
    1. Advertising

  2. Re: document.all[txtObj] works but document.getElementById[txtObj]fails

    wk wrote:


    > in my code
    > document.all[txtObj].value works but
    > document.getElementById[txtObj].value fails.


    document.getElementById is a function you should call in the form
    document.getElementById('string-with-element-id')
    so throw out the square brackets you have and correctly call
    document.getElementById().


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 22, 2005
    #2
    1. Advertising

  3. wk

    Guest

    hi wk,

    getElementById() gets only one element that u specified with its id i
    think, so it must be used something like this.
    function foo(txtObj) {//txtObj is the objects id
    var val=document.getElementById(txtObj).value;
    //...;
    }

    wk wrote:
    > hi,
    > in my code
    > document.all[txtObj].value works but
    > document.getElementById[txtObj].value fails.
    >
    >
    > any ideas why?
    , Apr 22, 2005
    #3
  4. wk

    wk Guest

    the problem is that the the txtObj part in
    document.getElementById[txtObj­].value, is coming froma string made of
    comma separated name of objects such as 'txtObj,txtObj2,txtABC'. So i
    need to split them, and them use them as actual objects IDs. How to go
    about it?

    document.getElementById(tx­tObj).value syntax fails, as txtObj is a
    string at the moment. Can i somehow convert the string or cast it to an
    object?
    wk, Apr 22, 2005
    #4
  5. Re: document.all[txtObj] works but document.getElementById[txtObj]fails

    "wk" <> writes:

    > document.getElementById(tx­tObj).value syntax fails, as txtObj is a
    > string at the moment.


    That is not why it fails. The argument to document.getElementById should
    be a string. If it fails, it's because the document doesn't contain an
    element with that id.

    (And please fix your news client to not insert arbitrary "-"'s in your
    text).
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 22, 2005
    #5
  6. wk wrote:

    > document.getElementById(tx­tObj).value syntax fails, as txtObj is a
    > string at the moment.


    document.getElementById(), as well as Microsoft's document.all()
    indeed do expect a string as (an optional first, in document.all's
    case) argument.

    If you have a comma separated list of IDs, and want to get an array
    of elements as a result, you might use something like:

    function getElementsByIds(sCSIDList) {
    var aRV = [], aIDs, i, j, o;
    if (typeof sCSIDList == "string") {
    aIDs = txtObj.split(/,/);
    if (document.getElementByID) {
    for (i=0; i<aIDs.length; i++) {
    aRV = document.getElementById(aIDs);
    }
    }
    else if (document.all) {
    // ...
    }
    }
    return aRV;
    }

    ciao, dhgm
    Dietmar Meier, Apr 22, 2005
    #6
    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. Jim Ley
    Replies:
    3
    Views:
    205
  2. lawrence
    Replies:
    3
    Views:
    193
  3. Derek Erb
    Replies:
    5
    Views:
    173
  4. Martin
    Replies:
    3
    Views:
    145
  5. Replies:
    4
    Views:
    160
    Jonas Raoni
    Nov 21, 2006
Loading...

Share This Page