Click to See Complete Forum and Search --> : Adding/Changing Text in a DIV tag


davidcholley
03-08-2003, 01:16 PM
Hello,

I'm trying to use the following function to change the text that lies between a DIV tag, however its not working any ideas...

function changeDateValue()
{
advisory = document.getElementById('div_w0d2');
advisory.removeChild(advisory.childNodes[0]);
advisory.appendChild(document.createTextNode('1'));
}

and the HTML...

<table border="1">
<tr>
<td width="25" id="td_w0d0"><div id="div_w0d0"></div></td>
<td width="25" id="td_w0d1"><div id="div_w0d1"></div></td>
<td width="25" id="td_w0d2"><div id="div_w0d2"></div></td>
<td width="25" id="td_w0d3"><div id="div_w0d3"></div></td>
<td width="25" id="td_w0d4"><div id="div_w0d4"></div></td>
<td width="25" id="td_w0d5"><div id="div_w0d5"></div></td>
<td width="25" id="td_w0d6"><div id="div_w0d6"></div></td>
</tr>
</table>

When the function executes, the IDEA is to place a '1' in the div tag and thus in corresponding table cell, however I keep getting the cryptic message:

Error: uncaught exception: [Exception... "Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER) [nsIDOMHTMLDivElement.removeChild]" nsresult: "0x80004003 (NS_ERROR_INVALID_POINTER)" location: "JS frame :: file:///I:/developement/datepicker.html :: changeDateValue :: line 36" data: no]

pyro
03-08-2003, 01:19 PM
You can do it like this:

document.getElementById('div_w0d2').innerHTML = "1";

khalidali63
03-08-2003, 01:39 PM
:D

Hey how about this.


<script type="text/javascript">
function changeDateValue(){
var nl = document.getElementsByTagName("td");
for(x=0;x<nl.length;x++){
obj = nl[x];
obj.innerHTML = (x+1);
}
}
window.onload = changeDateValue;
</script>
</head>
<body>

<table border="1">
<tr>
<td width="25" id="td_w0d0"></div></td>
<td width="25" id="td_w0d1"></div></td>
<td width="25" id="td_w0d2"></div></td>
<td width="25" id="td_w0d3"></div></td>
<td width="25" id="td_w0d4"></div></td>
<td width="25" id="td_w0d5"></div></td>
<td width="25" id="td_w0d6"></div></td>
</tr>
</table>


Cheers

Khalid

davidcholley
03-08-2003, 01:53 PM
If I'm correct .innerHTML isn't a standard and thus will only work for MSIE.

pyro
03-08-2003, 01:54 PM
.innerHTML will also work in NN/Mozilla, etc.

.innerTEXT is IE only.

khalidali63
03-08-2003, 01:59 PM
The solution I posted is both IE and NS6+ compatible it is DOM based so the
"Code of the future" as some may call it..

:D

Khalid

pyro
03-08-2003, 02:09 PM
The code I posted will work in IE6, Opera 7, NN7, and Mozilla 1.2, and probably more... :D