Hopeless

ronaldroe

Super Moderator
Staff member
getElementById for a noob

I'm trying really hard to learn JS, but it just isn't going well.

That said, here's what I'm trying to do. I have a client who wants to say something to the effect that he's been performing magic for x years. I'd like to create a script that, if I input the date he started performing, would calculate the number of years since, so it won't need to be updated in the future.

I've found and edited the following script to that end:
Code:
<script type="text/javascript">

var millennium =new Date(1971, 3, 25);
today=new Date();

var one_day=1000*60*60*24*365;

document.write(Math.ceil((today.getTime()-millennium.getTime())/(one_day))

</script>

I understand what's going on here.

However, I'd like, instead of writing the answer with document.write, to target a span using getElementById. ...And that's where I'm lost.

Please help :confused:
 
Last edited:

leroy30

New Member
Easy peasy :eek:)

To target a span (or any other html element) using the id you need to go like this...

document.getElementById('myid') then dot whatever attribute or method you wish to access.

To fill the contents of a span you can use innerHTML.

So you would give your span a valid id like this:

<span id="myid">over 40 years</span>

then change the text by doing this...

document.getElementById('myid').innerHTML = Math.ceil((today.getTime()-millennium.getTime())/(one_day)));

NB.. See how I put 'over 40 years' as the default - you'll want something like that in case the user has javascript disabled.

Let me know how that goes :)
 

ronaldroe

Super Moderator
Staff member
Thanks for your help. It's still not working. I can only figure that I must have jacked something up when I edited the original.
 

ronaldroe

Super Moderator
Staff member
There is no web page. I'm waiting on the client for content so I can finish the site. I just had an idea to do this.

@websonalized: I'll take a look at Lynda.com - thanks!
 

leroy30

New Member
What browser are you using? If you are using IE then open up developer tools or firebug in mozzila and have a look under scripts to see if there are any javascript errors...
 

ronaldroe

Super Moderator
Staff member
Here's what I get from IE developer tools:

SCRIPT5007: Unable to set value of the property 'innerHTML': object is null or undefined
 

DHDdirect

New Member
Hey Ron.. have you checked out JSFiddle yet?

It will allow you to share your code with others so we can troubleshoot with you. It's pretty handy because it shows you a four windowed browser with JS, CSS, HTML, and Preview.
 

ronaldroe

Super Moderator
Staff member
I have, I just didn't think about doing that.

I'll put it up there and post it on here today. Thanks!
 

leroy30

New Member
Sound's like it's not finding the html element.

Where abouts in your code is this piece of javascript?

btw are you using jquery? If so then it's better practise to do this...

$(document).ready(function() {

// your code to run here will start after the page has finished loading

});

or if you aren't then in your body tag call a js function in the onload event...

<body onload="startup();">......

</body>
<script type="text/javascript">

function startup() {
// your startup code here will run once the body has finished loading...
};

</script>

Your problem could be that the script is trying to modify the html element before it is being rendered which results in object reference not found.
 

ronaldroe

Super Moderator
Staff member
That was it! Thanks a million!

One last thing: It isn't rounding. I get nothing from IE dev console

EDIT: Disregard. I was missing a set of parenthesis...Thanks again!!!
 
Last edited:

anmolmark

New Member
It is a feeling that conditions will never improve, that there is no solution to a problem, and, for many, a feeling that dying by suicide would be better than living.

Most people who feel hopeless have depression, and untreated depression is the number one cause for suicide.

People often express hopelessness in statements that they make, such as the following:

* Things will never get better.

* There are no solutions to my problems.without hope because there seems to be no possibility of comfort or success
of a person unable to do something skillfully
certain to fail
 

prankpeter

New Member
Hopelessness: A Dangerous Suicide Warning Sign

Hopelessness: A Dangerous Suicide Warning Sign

by Kevin Caruso

The expression of hopelessness in conjunction with a mental disorder such as depression represents a very dangerous warning sign and always needs to be taken very seriously.

So what is hopelessness?

It is a feeling that conditions will never improve, that there is no solution to a problem, and, for many, a feeling that dying by suicide would be better than living.

Most people who feel hopeless have depression, and untreated depression is the number one cause for suicide.

People often express hopelessness in statements that they make, such as the following:

* Things will never get better.

* There are no solutions to my problems.

* I will never be happy again.

* I will never get over what happened.

* I don't see things ever improving.

* There is no point in trying anymore.

* I just want to give up.

* Things are hopeless.

* I feel so hopeless.

* There is no hope for me.

* What do I have to look forward to?

* The future is empty for me.

* I only see things getting worse in the future.

* Everything is going downhill.

* I will never get back to the way I was.

* It's too late for me.

* There is nothing that I can do to make things better.

Numerous studies have shown that feelings of hopelessness in conjunction with a mental disorder can lead to suicide. Two such studies were conducted at the University of Pennsylvania School of Medicine: one study tracked close to 2000 psychiatric outpatients deemed to be at risk for suicide, and the other tracked about 200 hospitalized psychiatric patients deemed to be at risk for suicide.

After patients were assessed, their levels of hopelessness -- based on a scale known as the "Beck Hopelessness Scale" - were recorded.

The individuals were tracked over the next several years, and, in both studies, significantly more suicides occurred in the group of individuals who exhibited the highest levels of hopelessness.

Thus, anyone expressing feelings of hopelessness who may be suffering from depression or a similar disorder needs to be evaluated as soon as possible.

Effective treatment can eliminate or substantially reduce feelings of hopelessness. Indeed, depression is highly treatable and the vast majority of people who receive treatment get better.

If you feel hopeless and think that you may be suffering from depression or a similar disorder, you need to reach out for help - and you need to do so now.

Please make an appointment with a medical doctor and a therapist so you may be evaluated.

And remember this: There always is hope. But you may not feel that hope until you receive effective treatment for any disorder that you might have.

If you or someone you know is suicidal, please go to the Home Page of this website for immediate help.

Thank you.

I love you.

Take care,

Kevin Caruso
 
Top