954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why wont this very simple javascript code work?

I'm completely new to javascript, but in my eyes this should work.
But then again, i'm quite an idiot myself and it's usually a very very simple problem that someone will see in 5 seconds. I want the header (supposed to be in variable 'itemname') to be put into variable item1, then the alertbox to come up with the string that was inserted into the variable.
Say if the header had the word 'Hello' in it, then when you click a button, 'Hello' would come up in an alert. And if the header had the word 'Goodbye', then the alert would say 'Goodbye'.

<script type="text/javascript">
function alert() {
var item1 = document.getElementById("itemname");
alert(item1);
}
</script>


(next snippet is the html)

<h1 id=”itemname”>
Hello
</h1>


Shouldn't this work? All it does is show 'null'.
Thanks for the help.


edit: lol, i was right. figured it out about 20 minutes after this thread. just remade the code exactly the same way and it worked. no clue what happened, but yeah. solved..

FriXionX
Newbie Poster
21 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Shouldn't your function recurse infinitely (at least until memory is exhausted)?

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 
Shouldn't your function recurse infinitely (at least until memory is exhausted)?

Oh woops, I forgot to include the button onclick thing in that post. Told ya I was dumb aha.

Don't worry, I fixed it some time ago, but thanks for looking out for that.

FriXionX
Newbie Poster
21 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You