Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #22690 > unrolled thread
| Started by | "Tony Johansson" <johansson.andersson@telia.com> |
|---|---|
| First post | 2014-01-18 19:29 +0100 |
| Last post | 2014-01-19 21:56 +0100 |
| Articles | 13 — 9 participants |
Back to article view | Back to comp.lang.javascript
How do I best find the last anchor under a table with id=filer "Tony Johansson" <johansson.andersson@telia.com> - 2014-01-18 19:29 +0100
Re: How do I best find the last anchor under a table with id=filer "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-18 20:28 +0100
Re: How do I best find the last anchor under a table with id=filer Danny <dann90038@gmail.com> - 2014-01-18 11:46 -0800
Re: How do I best find the last anchor under a table with id=filer Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-18 21:39 +0000
Re: How do I best find the last anchor under a table with id=filer Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-19 05:44 +0100
Re: How do I best find the last anchor under a table with id=filer Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-18 21:24 +0000
Re: How do I best find the last anchor under a table with id=filer Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-19 05:32 +0100
Re: How do I best find the last anchor under a table with id=filer John Harris <niam@jghnorth.org.uk.invalid> - 2014-01-20 15:34 +0000
Re: How do I best find the last anchor under a table with id=filer "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-20 16:38 +0100
Re: How do I best find the last anchor under a table with id=filer John Harris <niam@jghnorth.org.uk.invalid> - 2014-01-20 15:54 +0000
Re: How do I best find the last anchor under a table with id=filer RobG <rgqld@iinet.net.au> - 2014-01-19 16:40 +1000
Re: How do I best find the last anchor under a table with id=filer JJ <duh@nah.meh> - 2014-01-19 19:07 +0700
Re: How do I best find the last anchor under a table with id=filer Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-19 21:56 +0100
| From | "Tony Johansson" <johansson.andersson@telia.com> |
|---|---|
| Date | 2014-01-18 19:29 +0100 |
| Subject | How do I best find the last anchor under a table with id=filer |
| Message-ID | <lbeh71$38c$1@dont-email.me> |
There is always only one td in each tr and there is always the anchor
element in the td element
So I must find the last anchor element in the table with id=filer ?
What is the best way to do that in either javascript or jquery ?
<table id="filer">
<tbody>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_0"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl01$LinkButton1','')">281668_3983272262962_790318380_n.jpg
</a>
</td>
</tr>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_1"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl02$LinkButton1','')">bild.jpg
</a>
</td>
</tr>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_2"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl03$LinkButton1','')">img029.jpg
</a>
</td>
</tr>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_3"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl04$LinkButton1','')">img028.jpg
</a>
</td>
</tr>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_4"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl05$LinkButton1','')">img027.jpg
</a>
</td>
</tr>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_5"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl06$LinkButton1','')">img026.jpg
</a>
</td>
</tr>
<tr>
<td>
<a id="ContentPlaceHolder2_Repeater2_LinkButton1_6"
href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$Repeater2$ctl07$LinkButton1','')">img025.jpg
</a>
</td>
</tr>
</tbody>
</table>
//Tony
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2014-01-18 20:28 +0100 |
| Message-ID | <XnsA2B9D040594BCeejj99@194.109.133.133> |
| In reply to | #22690 |
"Tony Johansson" <johansson.andersson@telia.com> wrote on 18 jan 2014 in
comp.lang.javascript:
> There is always only one td in each tr and there is always the anchor
> element in the td element
> So I must find the last anchor element in the table with id=filer ?
> What is the best way to do that in either javascript or jquery ?
1 Asking for "the best way" of programming is nonsense, as that is subjective
if you do not add "the best way for using the least scripting characters" or
"... for processor speed".
2 Asking dor jquery can never be the best way and is not prudent to ask in
this NG.
3 Not telling whay you tried already could be construed as letting someone
else do your job.
4 Putting your whole code here is not necessary and therefore frowned upon,
show only the smallest code that your Q can be demonstrated on.
Try:
<table id='filer'>
<tr><td>aaa
<tr><td>bbb
<tr><td><a id='zzz' href='http://cnn.com/'>cnn</a>
</table>
<script type='text/javascript'>
var theTable = document.getElementById('filer');
var rowsCollection = theTable.rows;
var lastRow = rowsCollection[ rowsCollection.length - 1 ];
var firstCellLastRow = lastRow.cells[0];
var firstCellLastRowFirstAnchor =
firstCellLastRow.getElementsByTagName('a')[0];
alert(firstCellLastRowFirstAnchor.id);
alert(firstCellLastRowFirstAnchor.href);
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Danny <dann90038@gmail.com> |
|---|---|
| Date | 2014-01-18 11:46 -0800 |
| Message-ID | <83b86ec2-d252-45bb-a302-df2115ee86f4@googlegroups.com> |
| In reply to | #22690 |
To Tony Johansson
based on your markup structure
var tbl = document.getElementById('filer'),
lastLink = tbl.rows[tbl.rows.length-1].document.getElementsByTagName('a')[0]
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-18 21:39 +0000 |
| Message-ID | <lbes9n$to6$2@dont-email.me> |
| In reply to | #22702 |
On Sat, 18 Jan 2014 11:46:19 -0800, Danny wrote:
> To Tony Johansson
>
> based on your markup structure
>
> var tbl = document.getElementById('filer'),
> lastLink =
> tbl.rows[tbl.rows.length-1].document.getElementsByTagName('a')[0]
I don't think this needs the document in the second statement:
var tbl = document.getElementById('filer'),
lastLink = tbl.rows[tbl.rows.length-1].getElementsByTagName('a')[0];
You could also:
r = document.getElementById('filer').rows;
lastLink = r[r.length-1].getElementsByTagName('a')[0];
Or even:
links=document.getElementById('filer').getElementsByTagName('a');
lastLink=links[links.length-1];
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-01-19 05:44 +0100 |
| Message-ID | <14487076.kciktWhI2T@PointedEars.de> |
| In reply to | #22705 |
Denis McMahon wrote:
> On Sat, 18 Jan 2014 11:46:19 -0800, Danny wrote:
>> To Tony Johansson
>>
>> based on your markup structure
>>
>> var tbl = document.getElementById('filer'),
>> lastLink =
>> tbl.rows[tbl.rows.length-1].document.getElementsByTagName('a')[0]
>
> I don't think this needs the document in the second statement:
Does not *need*? Objects implementing the HTMLTableRow interface simply do
not have a “document” property in any known DOM implementation.
As a result, the property access results in “undefined” (if that), and this
unsurprisingly completely clueless code, by the incorrigible “Danny”
Googlodyte-wannabe haunting this newsgroup again, results in a *runtime
error* (TypeError).
> […]
> Or even:
>
> links=document.getElementById('filer').getElementsByTagName('a');
> lastLink=links[links.length-1];
The only one of your suggestions that makes sense, with you knowing nothing
about the table structure. However, it should be
var links = …;
var lastLink = …;
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-18 21:24 +0000 |
| Message-ID | <lberf9$to6$1@dont-email.me> |
| In reply to | #22690 |
On Sat, 18 Jan 2014 19:29:44 +0100, Tony Johansson wrote:
> There is always only one td in each tr and there is always the anchor
> element in the td element So I must find the last anchor element in the
> table with id=filer ? What is the best way to do that in either
> javascript or jquery ?
Don't know about "best", and don't know about jquery.
In javascript there are several ways, including (untested):
var t,c,a,e;
t=document.getElementById("filer"); // table
c=t.rows[t.rows.length-1].cells[0]; // only cell in last row
a=c.getElementsByTagName("a"); // anchors in cell
e=a[a.length-1]; // last anchor
If there's only ever a single anchor in each cell:
var t,c,e;
t=document.getElementById("filer"); // table
c=t.rows[t.rows.length-1].cells[0]; // only cell in last row
e=c.getElementsByTagName("a")[0]; // only anchor in cell
Possibly quicker, possibly not, may depend on browser:
var a,e;
a=document.getElementById("filer").getElementsByTagName("a"); // anchors
in table
e=a[a.length-1]; // last anchor
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-01-19 05:32 +0100 |
| Message-ID | <2281937.VQXnCUkh0a@PointedEars.de> |
| In reply to | #22704 |
Denis McMahon wrote: > On Sat, 18 Jan 2014 19:29:44 +0100, Tony Johansson wrote: >> There is always only one td in each tr and there is always the anchor >> element in the td element So I must find the last anchor element in the >> table with id=filer ? What is the best way to do that in either >> javascript or jquery ? > > Don't know about "best", and don't know about jquery. > > In javascript there are several ways, including (untested): > […] That is _not_ „javascript“; it is *the DOM*, a language-neutral API that provides interfaces to be implemented that have a binding for ECMAScript specified, among other programming languages. There is no “javascript”; there are ECMAScript implementations. The name of *one* of them, found in Mozilla-based software *only*, is (Netscape/Mozilla) JavaScript. Learn the difference, finally. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-01-20 15:34 +0000 |
| Message-ID | <ucgqd991dilk1tvihfps1nneikfjjuv334@4ax.com> |
| In reply to | #22707 |
On Sun, 19 Jan 2014 05:32:38 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:
>Denis McMahon wrote:
>
>> On Sat, 18 Jan 2014 19:29:44 +0100, Tony Johansson wrote:
>>> There is always only one td in each tr and there is always the anchor
>>> element in the td element So I must find the last anchor element in the
>>> table with id=filer ? What is the best way to do that in either
>>> javascript or jquery ?
>>
>> Don't know about "best", and don't know about jquery.
>>
>> In javascript there are several ways, including (untested):
>> […]
>
>That is _not_ „javascript“; it is *the DOM*, a language-neutral API that
>provides interfaces to be implemented that have a binding for ECMAScript
>specified, among other programming languages.
document.getElementById("filer")
is a perfectly good ECMAScript expression, sanctioned by ECMA 262
section 2, Conformance. Of course, what it does is not specified by
ECMA 262. It is defined, in a language independent way, in one of the
DOM standards.
Calling it an API is rather pretentious. It's like saying that the 'x'
in
x = 3;
is an API for accessing the value of a variable called 'x' : true, but
not helpful.
>There is no “javascript”; there are ECMAScript implementations. The name of
>*one* of them, found in Mozilla-based software *only*, is (Netscape/Mozilla)
>JavaScript.
For some reason, Thomas refuses to believe that the word 'javascript'
can be used to mean anything other than one particular language. Maybe
in German it is not possible for 'javascript' to mean 'any one of the
ECMAScript implementations', but this is an English language group and
it is possible.
>Learn the difference, finally.
I wish Thomas would.
John
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2014-01-20 16:38 +0100 |
| Message-ID | <XnsA2BBA93B9EABFeejj99@194.109.133.133> |
| In reply to | #22718 |
John Harris <niam@jghnorth.org.uk.invalid> wrote on 20 jan 2014 in comp.lang.javascript: > For some reason, Thomas refuses to believe that the word 'javascript' > can be used to mean anything other than one particular language. Maybe > in German it is not possible for 'javascript' to mean 'any one of the > ECMAScript implementations', but this is an English language group and > it is possible. This is NOT an English language group, this is a javascript language group. Even when you would "refuses to believe that". Using English here is just a conveniance, only PHP, C++ and alike are excluded. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-01-20 15:54 +0000 |
| Message-ID | <kjhqd95748so4cirnr735225c6runqk78b@4ax.com> |
| In reply to | #22707 |
On Sun, 19 Jan 2014 05:32:38 +0100, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: <snip> >> […] <snip> >> „javascript“ <snip> >> “javascript” <snip> Some time ago, Thomas insisted that Usenet is not restricted to the Internet; it can be accessed by other means. These other means include 7 bit + parity channels which are restricted to strict ASCII characters. So why is Thomas using characters that require an 8-bit channel? John
[toc] | [prev] | [next] | [standalone]
| From | RobG <rgqld@iinet.net.au> |
|---|---|
| Date | 2014-01-19 16:40 +1000 |
| Message-ID | <mv6dnXbtMMPs7kbPnZ2dnUVZ_t-dnZ2d@westnet.com.au> |
| In reply to | #22690 |
On 19/01/2014 4:29 am, Tony Johansson wrote:
> There is always only one td in each tr and there is always the anchor
> element in the td element
> So I must find the last anchor element in the table with id=filer ?
> What is the best way to do that in either javascript or jquery ?
As others have said, "best" is relative and needs some criteria to
determine. Often "best" means using code that works in as many user
agents as is reasonable, is easy to understand and maintain and does not
have obvious performance issues.
In this case, a function that fits those criteria might be:
function lastLink(id) {
var links, last;
var el = document.getElementById(id);
if (el) {
links = el.getElementsByTagName('a');
if (links.length) {
last = links[links.length - 1];
}
}
return last;
}
noting that not all A elements are (hyper)links, they may also be anchors.
Another, more problematic version that might fit a "less code" criterion is:
var lastLink =
[].pop.call(document.getElementById('filer').getElementsByTagName('a'));
However that will fail badly in IE 8 and lower and will throw an error
in all browsers if there is no element with ID 'filer' so is not to be
recommended.
--
Rob
[toc] | [prev] | [next] | [standalone]
| From | JJ <duh@nah.meh> |
|---|---|
| Date | 2014-01-19 19:07 +0700 |
| Message-ID | <1cm6ha8ko6mnt$.659tocexs4mp.dlg@40tude.net> |
| In reply to | #22690 |
On Sat, 18 Jan 2014 19:29:44 +0100, Tony Johansson wrote:
> There is always only one td in each tr and there is always the anchor
> element in the td element
> So I must find the last anchor element in the table with id=filer ?
> What is the best way to do that in either javascript or jquery ?
[snip]
Simplest one:
var eles = document.querySelectorAll("#filer a");
var lastAnchor = eles[eles.length-1];
[toc] | [prev] | [next] | [standalone]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-01-19 21:56 +0100 |
| Message-ID | <52dc3beb$0$6566$9b4e6d93@newsspool4.arcor-online.net> |
| In reply to | #22711 |
JJ wrote:
> On Sat, 18 Jan 2014 19:29:44 +0100, Tony Johansson wrote:
>> There is always only one td in each tr and there is always the anchor
>> element in the td element
>> So I must find the last anchor element in the table with id=filer ?
>> What is the best way to do that in either javascript or jquery ?
You probably mean "either with native DOM or jQuery methods". You may
consider reading the FAQ section "Language Overview"[1].
> [snip]
>
> Simplest one:
>
> var eles = document.querySelectorAll("#filer a");
> var lastAnchor = eles[eles.length-1];
Even simpler:
var lastAnchor = document.querySelector("#filer tr:last-child a");
Cf. Selectors API Level 1[2].
[1] <http://pointedears.de/scripts/faq/cljs/#ecma>
[2] <http://www.w3.org/TR/selectors-api/>
--
Christoph M. Becker
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web