Fetch the last tr in a table using prototype
The question is related to read td values using prototype
On a similar case, how do I fetch the last row in the following table,
&开发者_JAVA百科lt;table id="myTable">
<tr>
<td>apple</td>
<td>orange</td>
</tr>
<tr>
<td>car</td>
<td>bus</td>
</tr>
</table>
And how do you guys learn these methods and javascript libraries, I find it strenuous. The examples given in their website are not that good for a beginner :(
Thanks, J
As Dr.Molle just said, $$ returns an array. Knowing this you can come up with a shorter answer.
$$('#myTable tr').last()
And in my eyes is easier to read too without the [0]
. Because a table may have rows in a <tfoot>
as well I would stick a tbody
in that selector.
Personally I find Prototype easier to remember in general because it is easier to read then the supposedly simpler jQuery. In some way it better suits my programming style. That might be the secret, match a language/library/framework to your personality and not the other way around or you'll be needing therapy. You could validly ask the same on Programmers SE.
You can fetch the tr using:
$$('#myTable tr:last-child')[0]
$$ returns, different from $, an array(with the example above it contains 1 element), with [0]
you select the first element in this array.
Regarding to your additional question: prototypejs's documentation in my eyes is really bad, but there are frameworks with good api-documentations.
Maybe you better try another library(jQuery for example should be easy do understand, even to beginners, and is well documented too). I don't think that prototypejs, like it is now, will have a bright future.
精彩评论