473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add an onclick event

Hi Gurus

I have the following code:

function CI(ImgN, Alt){

if( document.getEle mentById && document.getEle mentById('il') ){

var d=document.getE lementById('il' );

if(document.cre ateElement){

var i=document.crea teElement('img' );

i.src="i/" + ImgN;

i.className='l' ;

i.alt=Alt;

while(d.firstCh ild!==null){

d.removeChild(d .firstChild);

}

d.appendChild(i );

return false;

}else if(d.innerHTML) {

d.innerHTML='<i mg src="i/' + ImgN + '" class="l" alt="' + Alt + '"
ONCLICK="return XI()">';

return false;

}else {

return true;}

} else {

return true;}

}

I now want to add the onclick event to the img (so that the user can close
it), how do I do that, or can I simply add a bit of text at the bottom of
the image 'close image' (and how do I do that??)?

TIA

- Nicolaas
Jul 23 '05 #1
10 3628
On Wed, 17 Nov 2004 23:16:41 +1300, WindAndWaves <ac****@ngaru.c om> wrote:
I have the following code:
Could you please indent code when you post it. It's difficult to read,
otherwise. I have some comments for it, though.

The first such comment is that your return values are backwards. I assume
that you're returning false upon success because you want to cancel an
event. In my opinion, the function should concern only itself - if, at a
higher level, you need the opposite result, use the NOT logical operator
(!) once the function has returned.
function CI(ImgN, Alt){
if(document.get ElementById && document.getEle mentById('il')) {
var d=document.getE lementById('il' );
This is rather inefficient. The first thing to do would be make a call to
gEBI reliable. At a simple level, that's:

if(!document.ge tElementById) {
document.getEle mentById = function() {return null;};
}

Emulation using document.all can be found in the FAQ notes[1], and in
posts from me in the past.

You could then change the first two lines to:

var d = document.getEle mentById('il');
if(d) {
if(document.cre ateElement) {
if(document.cre ateElement && d.appendChild && d.removeChild) {
var i=document.crea teElement('img' );
i.src="i/" + ImgN;
i.className='l' ;
i.alt=Alt;

while(d.firstCh ild!==null){
A strict comparison isn't necessary, and could be harmful - if
d.firstChild is undefined, the contents of the loop will be executed. The
standard inequality operator (!=) will avoid that.
d.removeChild(d .firstChild);
}
d.appendChild(i );
return false;
} else if(d.innerHTML) {
This isn't a sufficient test. Even if d.innerHTML was a string

} else if('string' == typeof d.innerHTML) {

there's no guarantee that writing to it will have any effect. The FAQ
notes[1] show a proper test.
d.innerHTML='<i mg src="i/' + ImgN + '" class="l" alt="'
+ Alt + '" ONCLICK="return XI()">';
return false;
} else {
return true;
}
} else {
return true;
}
}
The final improvement would be to have the function alter itself to
execute only one path, once support has been proven. However, that's
probably excessive.
I now want to add the onclick event to the img (so that the user can
close it),


The innerHTML path seems to accomplish that. To accomplish the same for
the DOM path, add

i.onclick = function() {return XI();};

[snip]

Hope that helps,
Mike
[1] Notes: <URL:http://www.jibbering.c om/faq/faq_notes/faq_notes.html>

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Michael Winter wrote:
[...]
function CI(ImgN, Alt){
[...]
+ Alt + '" ONCLICK="return XI()">';

[...]
i.onclick = function() {return XI();};


If the intention is to add the CI() function to the replacement image,
should this line be:

i.onclick = function() {return CI();};

or is XI() defined somewhere else?

Cheers, Rob
Jul 23 '05 #3
On Wed, 17 Nov 2004 21:39:50 +1000, RobG <rg***@iinet.ne t.auau> wrote:
Michael Winter wrote:


[snip]
i.onclick = function() {return XI();};


If the intention is to add the CI() function to the replacement
image, should this line be:

i.onclick = function() {return CI();};

or is XI() defined somewhere else?


My interpretation was that CI was called by clicking something, and it
adds an image to the document. The OP also wants a way to remove that
image: XI. A way to call XI is provided through the innerHTML path, but
not the DOM path.

I could be wrong. We'll just have to wait and see.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opshlxyqqr x13kvk@atlantis ...
On Wed, 17 Nov 2004 21:39:50 +1000, RobG <rg***@iinet.ne t.auau> wrote:
Michael Winter wrote:


[snip]
i.onclick = function() {return XI();};


If the intention is to add the CI() function to the replacement
image, should this line be:

i.onclick = function() {return CI();};

or is XI() defined somewhere else?


My interpretation was that CI was called by clicking something, and it
adds an image to the document. The OP also wants a way to remove that
image: XI. A way to call XI is provided through the innerHTML path, but
not the DOM path.

I could be wrong. We'll just have to wait and see.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Thank you both your help. It is looking great so far.
Jul 23 '05 #5
Nic, would this have worked for you?

- SimonFx

===

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT TYPE="text/javascript">
if(!document.ge tElementById) {document.getEl ementById = document.all ?
function(i) {return document.all[i];} : function() {return null;};}

function clearDIV (myDiv){
var obj = document.getEle mentById(myDiv) ;
obj.innerHTML = '';
obj.style.displ ay = 'none';
}

function blatIMG(myImage , myDiv){
var obj = document.getEle mentById(myDiv) ;
obj.innerHTML = '<IMG SRC="'+myImage+ '">';
obj.style.displ ay = '';
}

</SCRIPT>
</HEAD>
<BODY>

<a href="#" onClick="blatIM G('test01.jpg', 'myPic')">01</a>
<a href="#" onClick="blatIM G('test02.jpg', 'myPic')">02</a>
<a href="#" onClick="blatIM G('test03.jpg', 'myPic')">03</a>
<DIV ID="myPic" onClick="clearD IV('myPic')" STYLE="display: none"></DIV>
Blah
</BODY>
</HTML>
Jul 23 '05 #6
Ahem, maybe this would be better - works without Javascript.

===

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT TYPE="text/javascript">
if(!document.ge tElementById) {document.getEl ementById = document.all ?
function(i) {return document.all[i];} : function() {return null;};}

function clearDIV (myDiv){
var obj = document.getEle mentById(myDiv) ;
obj.innerHTML = '';
obj.style.displ ay = 'none';
}

function blatIMG(myImage , myDiv){
var obj = document.getEle mentById(myDiv) ;
obj.innerHTML = '<IMG SRC="'+myImage+ '">';
obj.style.displ ay = '';
return false;
}

</SCRIPT>
</HEAD>
<BODY>

<a href="test01.jp g" onClick="return blatIMG('test01 .jpg','myPic')" >01</a>
<a href="test02.jp g" onClick="return blatIMG('test02 .jpg','myPic')" >02</a>
<a href="test03.jp g" onClick="return blatIMG('test03 .jpg','myPic')" >03</a>
<DIV ID="myPic" onClick="clearD IV('myPic')" STYLE="display: none"></DIV>
Blah
</BODY>
</HTML>
Jul 23 '05 #7

Michael Winter wrote: while(d.firstCh ild!==null){
A strict comparison isn't necessary, and could be harmful - if
d.firstChild is undefined, the contents of the loop will be executed.
The
standard inequality operator (!=) will avoid that.
d.removeChild(d .firstChild);
}


Wouldn't this:

while (d.hasChildNode s())
d.removeChild(d .lastChild);

....be more utilitarian?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8
On 23 Nov 2004 16:45:19 GMT, Rob B <fe******@hotma il.com> wrote:

Are you misquoting me, or is it Developersdex?
Michael Winter wrote:
On Wed, 17 Nov 2004 23:16:41 +1300, WindAndWaves <ac****@ngaru.c om>
wrote:
while(d.firstCh ild!==null){


A strict comparison isn't necessary, and could be harmful - if
d.firstChild is undefined, the contents of the loop will be executed.
The standard inequality operator (!=) will avoid that.
d.removeChild(d .firstChild);
}


Wouldn't this:

while (d.hasChildNode s())
d.removeChild(d .lastChild);

...be more utilitarian?


Yes, that would do, too. :)

I don't know why I didn't suggest that; I did it recently.

while(d.firstCh ild) {/* ... */}

would have been better than a comparison, too.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9


Thanks. Don't think it was me, but you never know. New to these groups
(more au courant w/vBulletin) so struggling with the local customs.
Appreciate all your detailed responses. ;)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7032
by: Andreas Knollmann | last post by:
Hi, I create an object like this: var cell = document.createElement("td"). It doesn't have to be cell. I want this cell to use the onclick event. What doesn't work in the IE as well as with Mozilla is: cell.onclick = "whatever()";
2
18584
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
17
4887
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
6
9945
by: Cockroach | last post by:
Hello, I have a problem where the onClick of a table row will activates a window.location event, and inside a cell in that row, an image onClick event shows/hides a div. The problem is that when you click on the image, it briefly shows the div, and then reloads the page to the window.location url. Is there a way of preventing the onClick of the row (<tr>) from doing
4
2272
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML source:
5
2596
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below is the code for this. the onclick event calls a javascript function which I put an alert in the firt line to tell me if its working. It does work in IE. Any ideas on how to get netcrap... oops, I'm sorry, netscape to fire the onclick event? ...
5
5852
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. Thanks kai
7
2797
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The following is a snippet of the script. What is keeping it from working? function displaycards1(){ document.form1.reveal1.value="Play Again"; document.form1.reveal1.onClick="setcards();"; } </script>
4
13571
by: sameergn | last post by:
Hi, I have an image in my HTML form which has onclick() handler. There is also a submit button and a text box. Whenever text box has focus and user presses enter, the onclick() event of image is fired with event.keyCode as undefined. I was expecting that the form would get submitted. I tried returning from onclick() handler if keyCode is null, but the
5
13965
by: Stuart Shay | last post by:
Hello All I am working on ASP.NET 1.1 Custom Pager that allows a User to Enter a Number in a TextBox and go to the page selected. Since the OnClick Event does not work in ASP.NET 1.1 for a TextBox I want to use a hidden button to fire when the Onclick Event is fired for the TextBox.
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.