/    Sign up×
Community /Pin to ProfileBookmark

hellllllppppp!!! i must have no clue…

can someone explaine to me why this doesnt work??? i’ve read through 3 books on javascript before ever even attempting it. however, i’m realizing that none of what i do really works. so i’m guessing there was a chapter i may have missed and someone can help me. this isnt a script that im using, but it is an example that i made up to show you guys quickly what i am talking about.

<html>
<head>
<script type=”text/javascript”>
var doSomething=document.getElementById(“do”);
doSomething.onclick=getinfo;
function getinfo() {
alert(“talk about me”);
}

</script>
</head>

<body>

<a href=”#” id=”do”>whateveer</a>

</body>
</html>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 10.2010 — You're telling it to do something BEFORE you have told it where to do it from.

Try this two line change...
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=234134

onload = function() {
var doSomething=document.getElementById("do");
doSomething.onclick=getinfo;
}

function getinfo() { alert("talk about me"); }

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;a href="#" id="do"&gt;whateveer&lt;/a&gt;

&lt;/body&gt;
&lt;/html&gt;

?
Copy linkTweet thisAlerts:
@ShortsAug 10.2010 — Reorder:

[code=html]<html>

<body>

<a href="#" id="do">whateveer</a>
<script type="text/javascript">
var doSomething=document.getElementById("do");
doSomething.onclick=getinfo;
function getinfo() {
alert("talk about me");
}

</script>
</body>
</html>[/code]


Or add an onload:
[code=html]<html>
<head>
<script type="text/javascript">
window.onload = function() {
var doSomething=document.getElementById("do");
doSomething.onclick=getinfo;
function getinfo() {
alert("talk about me");
}
};
</script>
</head>
<body>

<a href="#" id="do">whateveer</a>

</body>
</html>[/code]

While you're trying to assign doSomething = 'do'; 'do' doesn't exist yet.

Edit: Yeah, what JMRKER said ?
Copy linkTweet thisAlerts:
@aqutenolejauthorAug 10.2010 — thanks.... so its the onload handler that i needed. but, if im not mistaken event handlers usually activates a function. like you have above. so does that mean that my entire code will be a sequence of functions? how would i ever be able to call a variable globally? or even better, how do i escape the function?

ex.
[CODE]
window.onload=goodness;
function goodness() {
document.write("hello world")
}

var a=6
var x=9
[/CODE]


in other words, if the browser needs me to tell it to wait til the page has loaded before executing my script, will what comes after the function that was executed through the onload handler happen also after the page has loaded? or will it make the two variables first then call the onload?
Copy linkTweet thisAlerts:
@ShortsAug 10.2010 — The onload will execute that function after the page is completely loaded (images included) so it will run only once. You don't need to use an onload for this example, you just need to make sure that the 'do' exists in the DOM before your try to read it using getElementById('do').

To work with a global variable, initiate it globally and then inside the function use it:

[code=html]<script type="text/javascript">
var doSomething;
window.onload = function() {
doSomething=document.getElementById("do");
doSomething.onclick=getinfo;
function getinfo() {
alert("talk about me");
}
};
</script>[/code]


What you could do is also set up a setTimeout to see if document.getElementById("do") exists yet. However, the best would probably just put at the bottom of your <BODY> without the onload as it will be available instantly.
Copy linkTweet thisAlerts:
@JMRKERAug 10.2010 — Try this as a test...
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=234134

// Try substituting next line for below as a second text
// var a=6; var x=9;

onload = function() {
var doSomething=document.getElementById("do");
doSomething.onclick=getinfo;
}

function getinfo() { alert("a = "+a+"nx = "+x); }

var a=6; var x=9;
// Remove above to see effects of moving global variables

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;a href="#" id="do"&gt;whateveer&lt;/a&gt;

&lt;/body&gt;
&lt;/html&gt;

You should see no difference with the substitutions indicated.
Copy linkTweet thisAlerts:
@Sterling_IsfineAug 11.2010 — [CODE]<html>
<body>

<a href="#" id="do">whatever</a>

<script type="text/javascript">
var doSomething=document.getElementById("do");
doSomething.onclick=getinfo;
function getinfo() {
alert("talk about me");
}

</script>
</body>[/CODE]
×

Success!

Help @aqutenolej spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...