Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

heading & variable 1

Status
Not open for further replies.

inma

Technical User
Apr 5, 2001
49
0
0
ES
I have a javascript function that gets the actual year in a variable.
I want to display that variable in a heading H1 ¿how to do it?
Thanks for your reply
 
1. Add an id to the heading, so that JavaScript can reference that element:
<h1 id="myspecialheading">Heading</h1>

2. Change innerHTML for that heading through JS:
Code:
<script type="text/javascript">
 var myVarYear = '';
 var h1 = document.getElementById('myspecialheading');
 h1.innerHTML = myVarYear;
</script>
Something like that.
 
Thanks Vragabond for your reply.
I have this script:
<script type="text/javascript">
var mydate = new Date()
var year = mydate.getFullYear()
var h1 = document.getElementById("putyear")
h1.innerHTML = year
</script>

And the heading in the HTML document is:
<h1 id="putyear">Heading</h1>

but it doesn't work.

 
Two suggestions:

Make sure the page is loaded before executing the JAvascript. You can't change an element that hasn't been created yet.

Which browser are you using? I don't think Firefox (Mozilla) supports the innerHTML attribute. Don't know about NN.


Meddle not in the affairs of dragons,
for you are crunchy, and good with mustard.
 
I put the script after <body>.
The browser is IE 6.00
 
WHERE after <body> did you put it? It should ideally be called either after the </body> tag, or from the body tags onLoad event.

Meddle not in the affairs of dragons,
for you are crunchy, and good with mustard.
 
This works for me in Mozilla, IE and Opera:
Code:
<html>
  <head>
    <title>Change Date</title>
    <script type="text/javascript">
      function fnChangeDate() {
        var mydate = new Date();
        var year = mydate.getFullYear();
        var h1 = document.getElementById("putyear");
        h1.innerHTML = year;
      }
    </script>
  </head>
  <body onload="fnChangeDate()">
    <h1 id="putyear">Heading</h1>
  <body>
</html>
 
Thanks Vragabond, it works perfectly.
 
I'm assuming the other variables to be concatenated with commas etc to become 'year' might be as follows?
Code:
<html>
	<head>
		<title>
			Change Date
		</title>
<script type="text/javascript">

[tab]function fnChangeDate() {
[tab][tab]var mydate = new Date();
[tab][tab]var day = mydate.getDay();
[tab][tab]var month = mydate.getDate();
[tab][tab]var year = mydate.getFullYear();
[tab][tab]var hours = mydate.getHours();
[tab][tab]var minutes = mydate.getMinutes();
[tab][tab]var millisecs = mydate.getMillseconds();
[tab][tab]var h1 = document.getElementById("putyear");
[tab][tab]h1.innerHTML = year;
[tab]}

</script>

	</head>
	<body onload="fnChangeDate()">
		<h1 id="putyear">
			Heading
		</h1>
	</body>
</html>


FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander[sup]tm[/sup].net
PDFcommander[sup]tm[/sup].co.uk
 
I guess this hasn't been answered yet. Unfortunately, you cannot format it that easy, since JS cannot get words like day names (Monday, Tuesday, ...) or month names (January,...) out of the date object. You have to work with arrays of your own names. However, you could do JS's default date output by just outputting mydate in that code. To answer your question though... concateing symbol in JS is + sign. You would join the string by doing:

date = day + month + year + hours + minutes;

Don't forget about styling, adding spaces and commas.
 
Code:
  function GetDay(intDay){
    var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
                         "Thursday", "Friday", "Saturday")
    return DayArray[intDay]
    }

  function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December") 
    return MonthArray[intMonth] 	  	 
    }
  function getDateStrWithDOW(){
    var today = new Date()
    var year = today.getYear()
    if(year<1000) year+=1900
    var todayStr = GetDay(today.getDay()) + " " + today.getDate() + " " + GetMonth(today.getMonth()) + " " + year;
    return todayStr
    }

call the function getDateStrWithDOW() to print the date
Code:
document.write(getDateStrWithDOW());
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top