Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var day = document.getElementById("day");
var holiday = new Date("October 30, 2012 12:55:00");


HTML
<table border="3px" align="center" style="border-color:black; border-style:solid; border-collapse:collapse; text-align:center;">
	<tr>
		<td width="100px";>days</td>
	</tr>
	<tr>
		<td id="day" width="100px";>here ist a space with html code (  ;)</td>
	</tr>
</table>


How can I write in the table div with the id day the variable holiday?
Posted
Updated 5-Sep-12 10:28am
v3

 
Share this answer
 
Comments
Andrei Straut 5-Sep-12 15:55pm    
We posted at the same time. I'll leave my solution here instead of deleting though, maybe it'll help someone else, as well as providing direct access to it. I've upvoted yours too.
Kuthuparakkal 5-Sep-12 22:58pm    
lot more stuff, my 5+
You can try this:
JavaScript
document.getElementById("day").innerHTML = holiday;

Also, you can find innerHTML reference here[^]

Furthermore, if you don't access the particular table cell in any other location, your can get rid of this whole line
var day = document.getElementById("day");
 
Share this answer
 
v2
Comments
Joan M 5-Sep-12 15:57pm    
Nice, direct and clear answer... and yes... completely at the same time... :D 5ed too.
Chi Ller 5-Sep-12 16:04pm    
so I should be able to write
day.innerHTML = (holiday);
if I want to have it in two steps. Am I right?
Andrei Straut 5-Sep-12 16:08pm    
Yes, definitely. I suggested deleting that line because I was thinking of simplifying it a little bit as well as eliminating possible variable scope issues (Javascript variable is, well...pretty unintuitive), but if you want it in 2 steps, that would be ok as well.

You can find a fairly nice scope demo here[^]
Chi Ller 5-Sep-12 16:12pm    
As far as i know your answer should work :D but it doesn´t :/ And if I do it in one step like you write it, it doesn´t work too.
Is it important to write it without brackets?
Andrei Straut 5-Sep-12 16:17pm    
Oh heck, I missed the Date datatype on your variable, your errors will most likely come from there.
Try this instead: document.getElementById("day").innerHTML = holiday.toString();

Also, Date object reference here[^]

Other that that, you may want to fire up the developer console and see the error text (if any) on your page. Just press F12 in your browser, and check there
A test/demo for how to write to the table cell (full HTML doc):

HTML
<html>
  <head>
    <script type="text/javascript">
	  var day = document.getElementById("day");
	  var holiday = new Date("October 30, 2012 12:55:00");
			
	  function writeToField() {
	    document.getElementById("day").innerHTML = holiday.toString();
	  }
    </script>
  </head>
  
  <body>
    <table border="3px" align="center" style="border-color:black; border-style:solid; border-collapse:collapse; text-align:center;">
	  <tr>
	    <td width="100px">days</td>
	  </tr>
	  <tr>
	    <td id="day" width="100px"> </td>
	  </tr>
    </table>
		
    <input type="button" id="testButton" value="Test"  onclick="writeToField();">
  </input></body>
</html>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900