Jukka said:
<script>
function letters(a,b,c) {
document.getElementById('box').innerHTML =
'<td>' + a + '</td><td>' + b + '</td><td>' + c + '</td>' ;
}
</script>
Change the <tr> tag for the table to <tr id=box>. (Here and before "box"
is just an identifier; choose another one if you can find a more
self-documenting name.)
Unreliable. Especially MSHTML (the layout engine that Internet Explorer
uses) is prone to errors with `innerHTML' accesses in table structures.
A safer approach is to remove all cells of the row except one, then replace
the content of the first cell and add more cells:
var tr = document.getElementById("box");
var firstChild = tr.firstChild;
while (tr.lastChild != firstChild)
{
tr.removeNode(tr.lastChild);
}
tr.cells[0].innerHTML = a;
var td1 = document.createElement("td");
td1.innerHTML = b;
/* or td1.cloneNode(false) */
var td2 = document.createElement("td");
td2.innerHTML = c;
tr.appendChild(td1);
tr.appendChild(td2);
(It suffices here to add text nodes to the cells, or replace their values if
existing, instead of using the proprietary and error-prone `innerHTML'
property. This can be accomplished using the `textContent' property of DOM
Level 3 Core or the document.createTextNode(…) and appendChild(…) methods of
DOM Level 2+ Core.)
Another possibility is to rewrite the entire table with `innerHTML'.
Quirksmode.org results have shown that this can be much more efficient than
the standards-compliant DOM approach. But probably not in this case.
Add onclick attributes to the <input> element to make them as follows:
box #1: <input type="text" value='x y z' size='10'
onclick="letters('x','y','z')" /><br />
box #2: <input type="text" value='a b c' size='10'
onclick="letters('a','b','c')" /><br />
box #3: <input type="text" size='10' onclick="letters('','','')" />
Since the value of the control can be used to construct the argument list
for the letters() method, and the `click' event bubbles universally, it
might not be necessary that each `input' element has an `onclick' attribute
specified. For example:
<script type="text/javascript">// <![CDATA[
function clickHandler(e)
{
var t = e.target || e.srcElement;
if (t)
{
if (t.nodeType == 3)
{
/* if a text node */
t = t.parentNode;
}
if (t.tagName.toUpperCase() == "INPUT")
{
letters.apply(null, t.value.split(" "));
}
}
}
// ]]></script>
<… onclick="if (typeof event != "undefined") clickHandler(event)" …>
<input type="text" value='x y z' size='10' /><br />
<input type="text" value='a b c' size='10' /><br />
<input type="text" size='10' value=' ' />
</…>
Usability, in particular keyboard navigation, might be better served by
listening to the not universally bubbling `focus' event instead, though.
For that matter, formatting should not be achieved using the `br' element,
but a semantic element, for example `label' elements that are parent
elements of their `input' elements.
PointedEars