Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old June 4th, 2008, 04:02 AM
Registered User
 
Join Date: Jun 2008
Location: , , .
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");
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

  #2 (permalink)  
Old June 4th, 2008, 05:53 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This worked for me:
Code:
<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)
  #3 (permalink)  
Old June 4th, 2008, 04:05 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
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
Default

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("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()
{
document.getElementById("txt2").value=22;

}
</script>

Hope this will help you


Cheers :)

vinod
  #4 (permalink)  
Old June 4th, 2008, 08:29 PM
Registered User
 
Join Date: Jun 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to pecal
Default

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.

  #5 (permalink)  
Old November 24th, 2008, 07:24 PM
Registered User
 
Join Date: Nov 2008
Location: , , United Kingdom.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:

Code:
    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!

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

Code:
    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...






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.