|
Subject:
|
onClick dose not work
|
|
Posted By:
|
pecal
|
Post Date:
|
6/4/2008 4:02:27 AM
|
Following is my code: method one:function parseSubTree(id){ var el= document.getElementById(id); var images = el.getElementsByTagName("IMG"); images[0].onClick=function(){showHide(id);}; } 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
|
|
Reply By:
|
joefawcett
|
Reply Date:
|
6/4/2008 5:53:42 AM
|
This worked for me:
<html>
<head>
<title>Image Click</title>
<style type="text/css">
</style>
<script type="text/javascript">
function addImageClick()
{
var img = document.images[0];
img.onclick = function(){alert(event.srcElement.src);};
}
</script>
</head>
<body onload="addImageClick();">
<img src="Borovets/Borovets1.jpg"></img>
</body>
</html>
--
Joe (Microsoft MVP - XML)
|
|
Reply By:
|
vinod_yadav1919
|
Reply Date:
|
6/4/2008 4:05:26 PM
|
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 *************test1.html************** <img id="myImg1"> <input type=text id="txt1">
<script>
document.getElementById("myImg1").onclick=function() {fun1();};
//fun1 will be called once you click on the image
function fun1() { document.getElementById("txt1").value=11;
} </script>
*************test2.html************** <img id="myImg2"> <input type=text id="txt2"> <script>
document.getElementById("myImg2").setAttribute("onclick",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() { document.getElementById("txt2").value=22;
} </script>
Hope this will help you
Cheers :)
vinod
|
|
Reply By:
|
pecal
|
Reply Date:
|
6/4/2008 8:29:22 PM
|
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.
|
|
Reply By:
|
Bricktop
|
Reply Date:
|
11/24/2008 6:24:58 PM
|
Hi,
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]);
target.setAttribute('onclick',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.
Thanks, Brick...
|