views:

415

answers:

2

hey I know I may seem like a complete fool for asking this question but I truly have never known and nothing I find helps me. I have this string generated using javascript and I want to append it to my existing web page on the fly. I've used document.write(); or just document.getElementbyId('').append(); and I just can't get it to append the document to my selected id...

I have:

<p><div id="dT">Bienvenido, Hoy es :: <div id="fecha" class="reloj"></div></div></p>

and I want to append the resulting string after the ::

The js is in a separate file but it starts working onload.


Edit to answer

I hadn't, but I just tried, still nothing happen, not even an error =/ this is what I did:

x = Hoy + " " + es + " dia " + dia + " del " + yr;

document.getElementbyId('dT').innherHTML += x;
+1  A: 

Seems that you have some typos, JS is case sensitive, the correct function name is getElementById (uppercase "B") and you have to set the innerHTML attribute not "innherHTML":

document.getElementById('dT').innerHTML += x;
CMS
Watch out for cross browser issues with getElementById. Libraries such as jQuery, Prototype etc remove a lot of these pains for you.
Shaun
Stop pushing damn libraries! Especially for "getElementById"!!!
J-P
A: 

If you want to put the text there while the page is loading, you use document.write:

<p><div id="dT">Bienvenido, Hoy es :: <div id="fecha" class="reloj">
<script type="text/javascript">

x = Hoy + " " + es + " dia " + dia + " del " + yr;

document.write(x);

</script>
</div></div></p>

You can also set the innerHTML property, but then you have to make sure that the script runs after the element has been read by the browser. Also you have to use the id of the element in the getElementById call:

x = Hoy + " " + es + " dia " + dia + " del " + yr;

document.getElementById('fecha').innerHTML = x;

Edit:
If you want the text to displayed on the same line, you may want to change the div to a span:

<p><div id="dT">Bienvenido, Hoy es :: 
<span id="fecha" class="reloj"></span>
</div></p>
Guffa