?
Solved

document.getElementById('myID').onclick ?

Posted on 2005-04-05
5
Medium Priority
?
418 Views
Last Modified: 2012-08-14
This works:
<a id="myLink">link test</a>
<script>
document.getElementById('myLink').href="javascript:NewPop('http://mopweb05.domain.com/scripts/savesearch/SvSrch.pl?InmDB='+DbTi+'&InmHost='+window.location.hostname+'&InmBU='+BUrl+'&SvSrch='+mystr,'saveSrchWin')";
</script>

Is it possible to do more or less the same thing with onclick? If so, how? I can't get it to work.

<a href="javascript:void(0)" id="myLink">link test</a>

<script>
document.getElementById('myLink').onclick="javascript:NewPop('http://mopweb05.domain.com/scripts/savesearch/SvSrch.pl?InmDB='+DbTi+'&InmHost='+window.location.hostname+'&InmBU='+BUrl+'&SvSrch='+mystr,'saveSrchWin')";
</script>


0
Comment
Question by:GessWurker
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:Xxavier
ID: 13710949
<script>
document.getElementById('myLink').onclick="function x(){NewPop('http://mopweb05.domain.com/scripts/savesearch/SvSrch.pl?InmDB='+DbTi+'&InmHost='+window.location.hostname+'&InmBU='+BUrl+'&SvSrch='+mystr,'saveSrchWin');return false}";
</script>
0
 
LVL 3

Accepted Solution

by:
hackman_3vilGuy earned 1000 total points
ID: 13711002
function doSomethingClever()
{
NewPop('http://mopweb05.domain.com/scripts/savesearch/SvSrch.pl?InmDB='+DbTi+'&InmHost='+window.location.hostname+'&InmBU='+BUrl+'&SvSrch='+mystr,'saveSrchWin');
return false;
}

document.getElementById('myLink').onclick=doSomethingClever;
0
 

Author Comment

by:GessWurker
ID: 13711503
hackman_3vilGuy: Yours worked for me. Thanks!

Can't use any quotes though, right?

document.getElementById('myLink').onclick=doSomethingClever;  <--this works

document.getElementById('myLink').onclick="doSomethingClever";<--this doesn't work
document.getElementById('myLink').onclick="doSomethingClever()";<-- this doesn't work
document.getElementById('myLink').onclick=doSomethingClever();<--this doesn't work either


0
 
LVL 3

Expert Comment

by:hackman_3vilGuy
ID: 13711661
Note that in the registration of an event handler you do not use brackets (). The onclick method expects to be assigned an entire function. If you’d do

element.onclick = doSomething();

the function would be executed and its result would be registered to onclick. This is not what we want, we want the function to be executed when the event takes place. In addition the function has usually been written to expect an event and if we’d execute it without any such context it would get terribly confused and produce JavaScript errors.

Instead we copy the function doSomething() to the event handler in its entirety. We do not execute it yet, that should only happen when the event actually takes place.

see http://www.quirksmode.org/js/events_tradmod.html for more info
0
 

Author Comment

by:GessWurker
ID: 13712075
Thanks for the solution and the great explanation!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question