0

I have an element on my website that is hosted externally and referenced in a script on my page.

I want to automatically change the text within this content so that it always displays tomorrow's date.

Script for the widget:

<script src="//my.externalwidget.com/12345.js" type="text/javascript" charset="utf-8" async="async"></script>

HTML contained in external script:

<div id="changeHeaderOne">THIS SHOULD DISPLAY TOMORROW'S DATE</div>

My JS:

<script type="text/javascript">
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var dateParse = currentDate.toString();
var dayAbbr = dateParse.substr(0, 3);
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
document.getElementById("changeHeaderTwo").innerHTML = "Tomorrow's date is " + dayAbbr + " " + month + "/" + day + "/" + year;
document.getElementById("changeHeaderOne").innerHTML = "Tomorrow's date is " + dayAbbr + " " + month + "/" + day + "/" + year;

I know my script is working because it runs properly on #changeHeaderTwo (located in DOM) but not on #changeHeaderOne (externally hosted and loaded using the script on top).

Any ideas how to make this apply to the externally loaded code as well?

1 Answer 1

3

You are using document.getElementByClass() (which should really by document.getElementsByClassName()) when your div has an id. Use document.getElementById() instead.

Sign up to request clarification or add additional context in comments.

8 Comments

PZP Thanks for your response, I am using the ID. I don't know why I put class in the question. I edited my question to reflect how it actually is right now (still not working). Any suggestions?
You still have it wrong. It should be getElementById, not getElementByID. Capitalization matters :P
You are absolutely right, capitalization does matter! I have it capitalized correctly, I just have no idea how to type so I wrote the question wrong... again... (sorry!). So, it is capitalized correctly but still not working. See above, I edited the question again to reflect the correct code this time (literally copy/pasted from my site). Do you have any ideas why it may not be working?
And yes, there is a </script> tag at the end, its just not showing on stackoverflow for some reason
It looks from viewing the page source that the code is never actually loaded fully into the page! I didn't know that was even possible. I created a work-around by just hosting all of the files locally myself. Thanks for all your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.