473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

add an onclick event

Hi Gurus

I have the following code:

function CI(ImgN, Alt){

if( document.getElementById && document.getElementById('il') ){

var d=document.getElementById('il');

if(document.createElement){

var i=document.createElement('img');

i.src="i/" + ImgN;

i.className='l';

i.alt=Alt;

while(d.firstChild!==null){

d.removeChild(d.firstChild);

}

d.appendChild(i);

return false;

}else if(d.innerHTML){

d.innerHTML='<img 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 3597
On Wed, 17 Nov 2004 23:16:41 +1300, WindAndWaves <ac****@ngaru.com> 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.getElementById && document.getElementById('il')) {
var d=document.getElementById('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.getElementById) {
document.getElementById = 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.getElementById('il');
if(d) {
if(document.createElement) {
if(document.createElement && d.appendChild && d.removeChild) {
var i=document.createElement('img');
i.src="i/" + ImgN;
i.className='l';
i.alt=Alt;

while(d.firstChild!==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='<img 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.com/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.net.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.******@blueyonder.co.invalid> wrote in message
news:opshlxyqqrx13kvk@atlantis...
On Wed, 17 Nov 2004 21:39:50 +1000, RobG <rg***@iinet.net.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.getElementById) {document.getElementById = document.all ?
function(i) {return document.all[i];} : function() {return null;};}

function clearDIV (myDiv){
var obj = document.getElementById(myDiv);
obj.innerHTML = '';
obj.style.display = 'none';
}

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

</SCRIPT>
</HEAD>
<BODY>

<a href="#" onClick="blatIMG('test01.jpg','myPic')">01</a>
<a href="#" onClick="blatIMG('test02.jpg','myPic')">02</a>
<a href="#" onClick="blatIMG('test03.jpg','myPic')">03</a>
<DIV ID="myPic" onClick="clearDIV('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.getElementById) {document.getElementById = document.all ?
function(i) {return document.all[i];} : function() {return null;};}

function clearDIV (myDiv){
var obj = document.getElementById(myDiv);
obj.innerHTML = '';
obj.style.display = 'none';
}

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

</SCRIPT>
</HEAD>
<BODY>

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

Michael Winter wrote: while(d.firstChild!==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.hasChildNodes())
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******@hotmail.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.com>
wrote:
while(d.firstChild!==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.hasChildNodes())
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.firstChild) {/* ... */}

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
JRS: In article <41**********************@news.newsgroups.ws>, dated
Wed, 24 Nov 2004 01:45:16, seen in news:comp.lang.javascript, Rob B
<fe******@hotmail.com> posted :

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.


Study the frequently-posted newsgroup FAQ - the appropriate part should
appear today - and relevant links therein, and your problems will reduce
to those of implementation via the peculiar "service" you use.

Install, if possible, properly-designed newsreader software, and
implementation will then present no difficulty.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11

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

Similar topics

2
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...
2
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 } ...
17
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...
6
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...
4
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...
5
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...
5
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. ...
7
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...
4
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...

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.