Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Wanting to use getElementById()

Reply
Thread Tools

Wanting to use getElementById()

 
 
Tim Streater
Guest
Posts: n/a
 
      12-09-2011
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
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      12-09-2011
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)
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      12-09-2011
In article <Xns9FB6D66455B83eejj99@194.109.133.133>,
"Evertjan." <(E-Mail Removed)> 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
 
Reply With Quote
 
Swifty
Guest
Posts: n/a
 
      12-10-2011
On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
<(E-Mail Removed)> 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
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      12-10-2011
In article <(E-Mail Removed)>,
Swifty <(E-Mail Removed)> wrote:

> On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-10-2011
Swifty wrote on 10 dec 2011 in comp.lang.javascript:

> On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
> <(E-Mail Removed)> 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)
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      12-10-2011
In article <Xns9FB772084920Feejj99@194.109.133.133>,
"Evertjan." <(E-Mail Removed)> wrote:

> Swifty wrote on 10 dec 2011 in comp.lang.javascript:
>
> > On Fri, 09 Dec 2011 23:59:05 +0000, Tim Streater
> > <(E-Mail Removed)> 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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-10-2011
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)
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      12-10-2011
In article <Xns9FB7A466E1438eejj99@194.109.133.133>,
"Evertjan." <(E-Mail Removed)> 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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-10-2011
Tim Streater wrote on 10 dec 2011 in comp.lang.javascript:

> In article <Xns9FB7A466E1438eejj99@194.109.133.133>,
> "Evertjan." <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nikon D7000 - Using an ME-1 Microphone and Wanting Occasional Use ofPop-Up Flash Richard Sgrignoli Digital Photography 0 12-08-2012 11:24 PM
help for newbie , wanting to use a 0845 number then redirect to my mobile via voip or similar ...... Dave T UK VOIP 27 11-27-2007 09:10 AM
Wanting Certification-Where to start =?Utf-8?B?TUNFIE1hZG5lc3M=?= MCSE 22 03-28-2006 07:06 PM
RE: Wanting Certification-Where to start =?Utf-8?B?RGF2aWQgUm9kcmlndWV6?= MCSE 0 03-26-2006 10:45 PM
Question from a newbie about which VOIP is best to use when I am studying in Europe and wanting to call back home to Australia?...as well as a few other queations! Ura UK VOIP 2 01-16-2006 02:07 PM



Advertisments