Support Rules and Guidelines
Before you post your support topic, please remember to:
  • Read the support forum rules.
  • Check the wiki and use search to see if your problem can be answered there first.
  • Link your board please, and make sure that the problem on your board is visible to guests or provide a test account.
  • Do not post Board Wrappers and CSS unless requested. With a board link, this is not necessary.
  • Be as descriptive as you can and use as many details as possible when describing your problem.
  • Please briefly mark any links to forums that may not be work-safe.
This will help ensure your support issue will be attended to in a quick and efficient manner.

  ClosedNew TopicNew Poll

 Javascript newbie - code won't display [SOLVED]
Hey all.

I'm working on a forum and one of the features I want is going to require Javascript. I'm completely new to working with Javascript, and I cannot for the life of me figure out why my code isn't working.

Basically I want the current month and date to display on my site, but I do not want the year (for reasons).

My board:

I have the following javascript pasted in the head of my board wrapper below <% JAVASCRIPT %>:
var d = new Date();
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
document.getElementById("scmonth").innerHTML = months[d.getMonth()];
document.getElementById("scdate").innerHTML = d.getDate();

But when I type in the following into the body of my board wrapper, nothing shows up on the site:
<div id="scdate"></div>
<div id="scmonth"></div>

I know whatever it is, I'm doing something that can probably be easily fixed. I'm just at a loss for what that is. Any help is appreciated. Thanks!

This post has been edited by electrumowl: Feb 15 2018, 08:41 PM
Remember that JS is executed inline when it's not delayed, and if the elements in question do not exist before the script executes, it won't work correctly. Move the script to the bottom of your page, or put it behind a load event.

Also, JS date objects are zero-based, so you may need to add one to the month integer to get it to parse correctly. You can also use .toLocaleString to have it return the name of the month, without doing the interpretation you're doing now - although, either works so, whichever!

This post has been edited by Dusty: Feb 15 2018, 10:04 PM
Oh. Yup. That'll do it.

Thanks a billion, that was exactly the problem!

Marking as solved.
This topic has been solved. If you need anymore help please post a new thread. Thanks for choosing Jcink services.

email: :: blog: John C.
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Share this topic:
« Next Oldest | General Support | Next Newest »

Options ClosedNew TopicNew Poll