Wrox Programmer Forums
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 4th, 2008, 04:02 AM
Registered User
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to pecal
Default onClick dose not work

Following is my code:
method one:function parseSubTree(id){
var el= document.getElementById(id);
var images = el.getElementsByTagName("IMG");

method two:images[0].setAttribute("onClick",showHide(id)};
i used both methods ,but they all can't work
i can't solve it ,hope to get some hlep,thanks!
forget to say,i use IE7.0 browser,
the method two works in Firefox browser

Old June 4th, 2008, 05:53 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

This worked for me:
<title>Image Click</title>
<style type="text/css">
<script type="text/javascript">
function addImageClick()
  var img = document.images[0];
  img.onclick = function(){alert(event.srcElement.src);};

<body onload="addImageClick();">
<img src="Borovets/Borovets1.jpg"></img>

Joe (Microsoft MVP - XML)
Old June 4th, 2008, 04:05 PM
Friend of Wrox
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919

HI pecal!!
There are two things I observed!!

var el= document.getElementById(id);
var images = el.getElementsByTagName("IMG");
-->it should be var images = document.getElementsByTagName("IMG"); not the --> el.getElementsByTagName("IMG");

Please note the following things
1>javascript is case sensitive ,so "onclick" is not the "onClick".
2>there is a difference between obj.onclick and obj.setAttribute.

Please try the following example
<img id="myImg1">
<input type=text id="txt1">


document.getElementById("myImg1").onclick=function () {fun1();};

//fun1 will be called once you click on the image

function fun1()


<img id="myImg2">
<input type=text id="txt2">

document.getElementById("myImg2").setAttribute("on click",fun2());
//fun will be called while you above statement is execute
//and it will not be executed when you click on the image ..
function fun2()


Hope this will help you

Cheers :)

Old June 4th, 2008, 08:29 PM
Registered User
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to pecal

thank vinod_yadav1919
my problem was solved with your help ^_^
1¡¢the onClick shoud be onclick
but if use setAttribute("onclick",function(){alert("11111");} ) in IE7
the onclick has to be onClick,otherwise it does not work,
in IE6 and FireFox is ok ,i tested

2¡¢"var el= document.getElementById(id);
   var images = el.getElementsByTagName("IMG");" is right too.

finally,thank vinok_yadav1919 again.
i registered here yesterdaty ,my English is bad
hope everybody can help me since then.
if i can ,i will do spare no effort too.

Old November 24th, 2008, 07:24 PM
Registered User
Join Date: Nov 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts


I'm having issues with setting the value of onclick in a link element.

Having read this article and the other article it links too, I still dont have a solution.

It might be due to the fact that the function 'name' is being taken from an array of function names! See here:

    var hooks = new Array("TTselectAll","TTselectNone");

    for ( var i in hooks )
        var target = document.getElementById(hooks[i]);

As is often the case, it works fine in FF but not in IE6 or 7.

I have tried the following and this stops working in all browsers!

    target.onclick = function() {hooks[i]+"();"};
...and this...

    var tag = hooks[i]+"();";
    target.onclick = function() {tag};
If anyone can suggest a workround for both FF and IE6 and 7 I would be very grateful.


Similar Threads
Thread Thread Starter Forum Replies Last Post
onClick crmpicco Javascript How-To 1 March 21st, 2005 09:18 AM
onClick crmpicco HTML Code Clinic 2 February 24th, 2005 11:28 AM
onClick outside a layer markp Javascript How-To 1 July 8th, 2004 08:59 AM
GETDATE() dose not......... arshad mahmood SQL Server 2000 2 August 19th, 2003 07:57 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.