B
Ben Hallert
Hi guys!
I'm working on a little javascriptlet/greasemonkey script, and I've run
into a challenge that I think can be solved with walking the DOM, but I
feel like I'm kludging my way through and wondering if there might be a
better answer out there.
Here's the problem: I want to insertCell(0) into an existing table, but
the table doesn't have a NAME or ID. I don't have the option of adding
these (it's for Wikipedia) and would like to programatically navigate
my way to the table. The table is inside a FORM element that _does_
have an ID, so it's pretty close to a known entry point. I was
originally planning on using .firstChild a few times in a nested
sequence to work my way down to the table, but it's proving trickier
than I thought it would, hence my question here.
Here's the relevant snippet of HTML I'm working with:
---------
<form id="blockip" method="post"
action="/w/index.php?title=Special:Blockip&action=submit">
<table border='0'>
<tr>
<td align="right">User:</td>
<td align="left">
---------
I want to insertCell(0) a new cell into the first TR in the table, then
setAttribute it to a rowspan of 8. My original foray into walking (or
perhaps stumbling is a better choice) the DOM was something like this:
var parent_form = document.getElementById('blockip');
var spectable = parent_form.firstChild;
var spectr = spectable.firstChild;
var newcell = spectr.firstChild.insertCell(0);
It matches the nesting shown above, but it feels kludgy and has the
added benefit of not working.
For you folks out there that might find themselves in the same sort of
situation (you can't add the ID to the existing table, but you need to
interact with it), how would you resolve this? Am I on the right path,
or is there a smarter way to get from point A to point B?
The end result is that I want to add a cell to the far left of the
table then insert a select element I'm currently jamming underneath
into it for better presentation.
Thanks!
Ben
I'm working on a little javascriptlet/greasemonkey script, and I've run
into a challenge that I think can be solved with walking the DOM, but I
feel like I'm kludging my way through and wondering if there might be a
better answer out there.
Here's the problem: I want to insertCell(0) into an existing table, but
the table doesn't have a NAME or ID. I don't have the option of adding
these (it's for Wikipedia) and would like to programatically navigate
my way to the table. The table is inside a FORM element that _does_
have an ID, so it's pretty close to a known entry point. I was
originally planning on using .firstChild a few times in a nested
sequence to work my way down to the table, but it's proving trickier
than I thought it would, hence my question here.
Here's the relevant snippet of HTML I'm working with:
---------
<form id="blockip" method="post"
action="/w/index.php?title=Special:Blockip&action=submit">
<table border='0'>
<tr>
<td align="right">User:</td>
<td align="left">
---------
I want to insertCell(0) a new cell into the first TR in the table, then
setAttribute it to a rowspan of 8. My original foray into walking (or
perhaps stumbling is a better choice) the DOM was something like this:
var parent_form = document.getElementById('blockip');
var spectable = parent_form.firstChild;
var spectr = spectable.firstChild;
var newcell = spectr.firstChild.insertCell(0);
It matches the nesting shown above, but it feels kludgy and has the
added benefit of not working.
For you folks out there that might find themselves in the same sort of
situation (you can't add the ID to the existing table, but you need to
interact with it), how would you resolve this? Am I on the right path,
or is there a smarter way to get from point A to point B?
The end result is that I want to add a cell to the far left of the
table then insert a select element I'm currently jamming underneath
into it for better presentation.
Thanks!
Ben