Wanting to use getElementById()

Discussion in 'Javascript' started by Tim Streater, Dec 9, 2011.

  1. Tim Streater

    Tim Streater Guest

    My app displays a table in a browser window. The user has the option to
    display a different table by clicking a button. I effect this by
    switching table bodies - I have an array containing pointers to a set of
    tbodies.

    Now, while this all works OK, if I wish to search for a row in such a
    table body by id, I'm only using getElementById() on the tbody that
    happens to be in the DOM just now. If I want to look for a row in
    another tbody, I'm having to search for that by hand with a JavaScript
    loop, which I assume is a lot slower than using getElementById.

    To get round this, I'm thinking that if I can create another document, I
    could append the tbody I want to search to that and then use
    getElementById on the result. Something like:

    mydocPtr = document.createElement("document");
    mydocPtr.appendChild(tbodyPtr);
    rowPtr = mydocPtr.getElementById(someId);

    Can I create a document element in this way? Anything I should be wary
    of?

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 9, 2011
    #1
    1. Advertising

  2. Tim Streater

    Evertjan. Guest

    Tim Streater wrote on 09 dec 2011 in comp.lang.javascript:

    > My app displays a table in a browser window. The user has the option to
    > display a different table by clicking a button. I effect this by
    > switching table bodies - I have an array containing pointers to a set of
    > tbodies.
    >
    > Now, while this all works OK, if I wish to search for a row in such a
    > table body by id, I'm only using getElementById() on the tbody that
    > happens to be in the DOM just now. If I want to look for a row in
    > another tbody, I'm having to search for that by hand with a JavaScript
    > loop, which I assume is a lot slower than using getElementById.
    >
    > To get round this, I'm thinking that if I can create another document, I
    > could append the tbody I want to search to that and then use
    > getElementById on the result. Something like:
    >
    > mydocPtr = document.createElement("document");
    > mydocPtr.appendChild(tbodyPtr);
    > rowPtr = mydocPtr.getElementById(someId);
    >
    > Can I create a document element in this way? Anything I should be wary
    > of?


    Make up two tables [or tbody-s] one being display:none;-ed by default,
    and switch to display the other instead by javascript.

    function showTable(x) {
    document.getElementById('table1').style.display = 'none';
    document.getElementById('table2').style.display = 'none';
    document.getElementById(x).style.display = 'block';
    };


    Both their inividual td-elements stay accessable
    through their different getElementById()-s
    even when not displayed.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Dec 9, 2011
    #2
    1. Advertising

  3. Tim Streater

    Tim Streater Guest

    In article <Xns9FB6D66455B83eejj99@194.109.133.133>,
    "Evertjan." <> wrote:

    > Tim Streater wrote on 09 dec 2011 in comp.lang.javascript:
    >
    > > My app displays a table in a browser window. The user has the option to
    > > display a different table by clicking a button. I effect this by
    > > switching table bodies - I have an array containing pointers to a set of
    > > tbodies.
    > >
    > > Now, while this all works OK, if I wish to search for a row in such a
    > > table body by id, I'm only using getElementById() on the tbody that
    > > happens to be in the DOM just now. If I want to look for a row in
    > > another tbody, I'm having to search for that by hand with a JavaScript
    > > loop, which I assume is a lot slower than using getElementById.
    > >
    > > To get round this, I'm thinking that if I can create another document, I
    > > could append the tbody I want to search to that and then use
    > > getElementById on the result. Something like:
    > >
    > > mydocPtr = document.createElement("document");
    > > mydocPtr.appendChild(tbodyPtr);
    > > rowPtr = mydocPtr.getElementById(someId);
    > >
    > > Can I create a document element in this way? Anything I should be wary
    > > of?

    >
    > Make up two tables [or tbody-s] one being display:none;-ed by default,
    > and switch to display the other instead by javascript.
    >
    > function showTable(x) {
    > document.getElementById('table1').style.display = 'none';
    > document.getElementById('table2').style.display = 'none';
    > document.getElementById(x).style.display = 'block';
    > };
    >
    >
    > Both their inividual td-elements stay accessable
    > through their different getElementById()-s
    > even when not displayed.


    Mmmm, nice idea. I do have a variable number of these tables, but that
    wouldn't create an insurmountable problem. More of a problem is that I
    would then have id conflicts - each table has a similar set of row-id's.
    I expect I could get around this too at the expense of a certain amount
    of recoding.

    Is there anything wrong (or dangerous) about what I'm considering, that
    you know of?

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 9, 2011
    #3
  4. Tim Streater

    Swifty Guest

    On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
    <> wrote:

    >would then have id conflicts - each table has a similar set of row-id's.
    >I expect I could get around this too at the expense of a certain amount
    >of recoding.


    Faced with a similar problem, I ensured that the element ID's were
    unique by encoding the table number, the row number, and the column
    number in the ID, ending up with ID's such as C_5_217_9 (Cell in table
    5, row 217, colum 9).

    --
    Steve Swift
    http://www.swiftys.org.uk/swifty.html
    http://www.ringers.org.uk
     
    Swifty, Dec 10, 2011
    #4
  5. Tim Streater

    Tim Streater Guest

    In article <>,
    Swifty <> wrote:

    > On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
    > <> wrote:
    >
    > >would then have id conflicts - each table has a similar set of row-id's.
    > >I expect I could get around this too at the expense of a certain amount
    > >of recoding.

    >
    > Faced with a similar problem, I ensured that the element ID's were
    > unique by encoding the table number, the row number, and the column
    > number in the ID, ending up with ID's such as C_5_217_9 (Cell in table
    > 5, row 217, colum 9).


    Exactly.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 10, 2011
    #5
  6. Tim Streater

    Evertjan. Guest

    Swifty wrote on 10 dec 2011 in comp.lang.javascript:

    > On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
    > <> wrote:
    >
    >>would then have id conflicts - each table has a similar set of
    >>row-id's. I expect I could get around this too at the expense of a
    >>certain amount of recoding.

    >
    > Faced with a similar problem, I ensured that the element ID's were
    > unique by encoding the table number, the row number, and the column
    > number in the ID, ending up with ID's such as C_5_217_9 (Cell in table
    > 5, row 217, colum 9).


    That is so if you stick to individual ids [ ideas? ;-) ]
    of total gebi() control.

    However if the tables are roughly similar but for the content,
    why not:

    document.getElementById('table1').rows(217).cells(9)
    document.getElementById('table5').rows(217).cells(9)

    [rows and cells counts start at 0]

    =========================================

    Better use a function:

    function getTableCell(t,r,c) {
    return document.getElementById(t).rows(r).cells(c);
    };

    getTableCell('table1',217,9).style.color = 'green';
    getTableCell('table5',217,9).style.color = 'red';

    var theCell = getTableCell('table88',2,1);
    alert(theCell.innerHTML);
    theCell.style.color = 'red';

    var otherCell = getTableCell('table15',2,11);
    if (theCell.innerHTML != +otherCell.innerHTML+6)
    theCell.innerHTML = +otherCell.innerHTML+12;

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Dec 10, 2011
    #6
  7. Tim Streater

    Tim Streater Guest

    In article <Xns9FB772084920Feejj99@194.109.133.133>,
    "Evertjan." <> wrote:

    > Swifty wrote on 10 dec 2011 in comp.lang.javascript:
    >
    > > On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
    > > <> wrote:
    > >
    > >>would then have id conflicts - each table has a similar set of
    > >>row-id's. I expect I could get around this too at the expense of a
    > >>certain amount of recoding.

    > >
    > > Faced with a similar problem, I ensured that the element ID's were
    > > unique by encoding the table number, the row number, and the column
    > > number in the ID, ending up with ID's such as C_5_217_9 (Cell in table
    > > 5, row 217, colum 9).

    >
    > That is so if you stick to individual ids [ ideas? ;-) ]
    > of total gebi() control.
    >
    > However if the tables are roughly similar but for the content,
    > why not:
    >
    > document.getElementById('table1').rows(217).cells(9)
    > document.getElementById('table5').rows(217).cells(9)


    When I construct the table, I want to later be able to find a given row,
    so I can modify it. From the SQLite db I get the data from, I have a db
    name (or number) and the rowid. So far I've been making up an id for the
    row (as they are added to the tbody) just using the rowid, as in:

    id = 'M' + rowid;

    Simple enough to extend that to include the db number.

    But I'd still like to know whether:

    mydocPtr = document.createElement('document');

    is allowed.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 10, 2011
    #7
  8. Tim Streater

    Evertjan. Guest

    Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:

    > But I'd still like to know whether:
    >
    > mydocPtr = document.createElement('document');
    >
    > is allowed.
    >


    What would you want to use that for?

    As an equivalent for:

    document.open()
    document.write('....')

    ?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Dec 10, 2011
    #8
  9. Tim Streater

    Tim Streater Guest

    In article <Xns9FB7A466E1438eejj99@194.109.133.133>,
    "Evertjan." <> wrote:

    > Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:
    >
    > > But I'd still like to know whether:
    > >
    > > mydocPtr = document.createElement('document');
    > >
    > > is allowed.


    > What would you want to use that for?
    >
    > As an equivalent for:
    >
    > document.open()
    > document.write('....')


    As a document to which I can append a tbody, that I can then search with
    getElementById(), as I said in my first post.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 10, 2011
    #9
  10. Tim Streater

    Evertjan. Guest

    Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:

    > In article <Xns9FB7A466E1438eejj99@194.109.133.133>,
    > "Evertjan." <> wrote:
    >
    >> Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:
    >>
    >> > But I'd still like to know whether:
    >> >
    >> > mydocPtr = document.createElement('document');
    >> >
    >> > is allowed.

    >
    >> What would you want to use that for?
    >>
    >> As an equivalent for:
    >>
    >> document.open()
    >> document.write('....')

    >
    > As a document to which I can append a tbody, that I can then search with
    > getElementById(), as I said in my first post.


    Why would you want to append a tbody to a document?

    A tbody should only be appended to a table element, meseems.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Dec 10, 2011
    #10
  11. Tim Streater

    Tim Streater Guest

    In article <Xns9FB7C43B48B3Deejj99@194.109.133.133>,
    "Evertjan." <> wrote:

    > Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:
    >
    > > In article <Xns9FB7A466E1438eejj99@194.109.133.133>,
    > > "Evertjan." <> wrote:
    > >
    > >> Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:
    > >>
    > >> > But I'd still like to know whether:
    > >> >
    > >> > mydocPtr = document.createElement('document');
    > >> >
    > >> > is allowed.

    > >
    > >> What would you want to use that for?
    > >>
    > >> As an equivalent for:
    > >>
    > >> document.open()
    > >> document.write('....')

    > >
    > > As a document to which I can append a tbody, that I can then search with
    > > getElementById(), as I said in my first post.

    >
    > Why would you want to append a tbody to a document?
    >
    > A tbody should only be appended to a table element, meseems.


    I'm well aware of that, Evertjan. Stop being so perverse. I'd make the
    document, append a table and then put the tbody on that.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Dec 10, 2011
    #11
  12. Tim Streater

    Evertjan. Guest

    Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:
    >> Why would you want to append a tbody to a document?
    >>
    >> A tbody should only be appended to a table element, meseems.

    >
    > I'm well aware of that, Evertjan. Stop being so perverse. I'd make the
    > document, append a table and then put the tbody on that.


    Doesn't the document exist from the beginning of creation of your page?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Dec 10, 2011
    #12
  13. On Sat, 10 Dec 2011 12:16:47 +0000, Tim Streater wrote:

    > But I'd still like to know whether:
    >
    > mydocPtr = document.createElement('document');
    >
    > is allowed.


    I think that for an html document, document.createElement has to take a
    valid html element name.

    A simple empirical test suggests that it won't work in all browsers. ;)

    Rgds

    Denis McMahon
     
    Denis McMahon, Dec 10, 2011
    #13
  14. Denis McMahon wrote:

    > On Sat, 10 Dec 2011 12:16:47 +0000, Tim Streater wrote:
    >> But I'd still like to know whether:
    >>
    >> mydocPtr = document.createElement('document');
    >>
    >> is allowed.

    >
    > I think that for an html document, document.createElement has to take a
    > valid html element name.
    >
    > A simple empirical test suggests that it won't work in all browsers. ;)


    Please publish your results so that people who are still doing it are given
    something to think about.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Dec 10, 2011
    #14
  15. In comp.lang.javascript message <timstreater-095030.23590509122011@news.
    individual.net>, Fri, 9 Dec 2011 23:59:05, Tim Streater
    <> posted:

    >Mmmm, nice idea. I do have a variable number of these tables, but that
    >wouldn't create an insurmountable problem. More of a problem is that I
    >would then have id conflicts - each table has a similar set of row-
    >id's. I expect I could get around this too at the expense of a certain
    >amount of recoding.
    >
    >Is there anything wrong (or dangerous) about what I'm considering, that
    >you know of?


    My <http://www.merlyn.demon.co.uk/holidays.htm> has a number of tables
    constructed by writing their computed HTML. The tables are all
    essentially the same, but constructed in accordance with a data object
    which gives the number of rows and their content. One column can be re-
    computed by altering its heading cell. As I recall, or at least as I
    would do it now, all addressing of elements within a table is relative
    to that table, not global.

    If I were starting again, I'd build the table by DOM methods, as in js-
    props.htm or more accurately inc-prop.js.

    IMHO, getElementById is something like GO TO (GOTO?) in FORTRAN; one
    should use it as little as practical.

    --
    (c) John Stockton, nr London UK ?@merlyn.demon.co.uk IE8 FF8 Op11 Sf5 Cr15
    news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
    <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Dec 13, 2011
    #15
  16. On Tue, 13 Dec 2011 19:52:26 +0000, Dr J R Stockton
    <> wrote:

    [snip]

    >IMHO, getElementById is something like GO TO (GOTO?) in FORTRAN; one
    >should use it as little as practical.


    1) In very few cases are spaces significant in Fortran. You can use
    either. Note the change in the spelling of "Fortran". It used to be
    all caps. It is not now.

    2) Thank you for the excuse to not use getElementById.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Dec 14, 2011
    #16
  17. In comp.lang.javascript message <hrufe79d6t4i8pgvcukl6rc6rhre8kvj9p@4ax.
    com>, Tue, 13 Dec 2011 17:29:00, Gene Wirchenko <> posted:

    >On Tue, 13 Dec 2011 19:52:26 +0000, Dr J R Stockton
    ><> wrote:
    >
    >[snip]
    >
    >>IMHO, getElementById is something like GO TO (GOTO?) in FORTRAN; one
    >>should use it as little as practical.

    >
    > 1) In very few cases are spaces significant in Fortran. You can use
    >either. Note the change in the spelling of "Fortran". It used to be
    >all caps. It is not now.


    I ceased using FORTRAN long before any such new-fangled ideas.


    I recall one case where spacing was significant. This was in some
    version of HP FORTRAN, not that it matters. A colleague complained of
    an inscrutably failing COMMON statement. I don't recall the variable
    names, but the code resembled

    COMMON THIS, THAT, ..., ..., ..., THING, TOTHER,
    C SOMETHINGELSE

    but TOTHER just did not act as a COMMON, although all the others did.
    We stared at the screen in disbelief. Eventually I observed that TOTHER
    started in a position which might well have been column 73 or 74, and
    light dawned. Had he not put spaces after his commas ...

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Dec 15, 2011
    #17
  18. On Thu, 15 Dec 2011 21:51:49 +0000, Dr J R Stockton
    <> wrote:

    [snip]

    >I recall one case where spacing was significant. This was in some
    >version of HP FORTRAN, not that it matters. A colleague complained of
    >an inscrutably failing COMMON statement. I don't recall the variable
    >names, but the code resembled
    >
    > COMMON THIS, THAT, ..., ..., ..., THING, TOTHER,
    > C SOMETHINGELSE
    >
    >but TOTHER just did not act as a COMMON, although all the others did.
    >We stared at the screen in disbelief. Eventually I observed that TOTHER
    >started in a position which might well have been column 73 or 74, and
    >light dawned. Had he not put spaces after his commas ...


    Column 73 and on have bitten more than a few people.

    When I was taking a COBOL course in my diploma, I was very
    careful about column 73. Any other student would have been wondering
    what I was about.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Dec 15, 2011
    #18
    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. Lucia
    Replies:
    5
    Views:
    335
    Roedy Green
    Nov 3, 2003
  2. Tim Apple

    Wanting to Learn

    Tim Apple, Feb 4, 2004, in forum: C Programming
    Replies:
    25
    Views:
    794
    Dik T. Winter
    Feb 7, 2004
  3. Zeljko Vrba
    Replies:
    13
    Views:
    621
  4. arsl89

    wanting

    arsl89, Jul 18, 2006, in forum: Python
    Replies:
    7
    Views:
    477
    Patrick Bothe
    Jul 18, 2006
  5. jt
    Replies:
    3
    Views:
    963
    Keith Thompson
    May 23, 2005
Loading...

Share This Page