473,399 Members | 3,656 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,399 software developers and data experts.

onclick function

Hi everybody,

I'm generating some DIVs dinamically, inclusive of
an event function to run when the user clicks on them.

While the following lines work perfectly in FF 1.5,
I.E. 6.0 ignores the onclick function alltogether:
the flow never gets into it, as if it hadn't been registered
properly. Yet, an inspection of the DOM with the
developer toolbar, reveals that the function is correctly
associated with the onclick attribute.

This is the code:

var imgNumber = 1;
var imgDot = document.createElement('div');

var onClickFunction = "ss_displayNthPicture("+imgNumber+")";
imgDot.setAttribute("onclick",onClickFunction);

document.body.appendChild(imgDot);
Any idea? Am I doing something wrong with quotes
and double quotes maybe? Or are there limitations
on the events a dynamically generated DIV can trigger?

Thanks for your help.

Manu

Jul 23 '06 #1
4 2561
It's possible IE just doesn't like using the setAttribute property on
event handlers, maybe you should try using attachEvent to add the event
to the object after it is added to the DOM.

http://msdn.microsoft.com/workshop/a...ttachevent.asp

This of course will only work in the case of IE.

ma****@gmail.com wrote:
Hi everybody,

I'm generating some DIVs dinamically, inclusive of
an event function to run when the user clicks on them.

While the following lines work perfectly in FF 1.5,
I.E. 6.0 ignores the onclick function alltogether:
the flow never gets into it, as if it hadn't been registered
properly. Yet, an inspection of the DOM with the
developer toolbar, reveals that the function is correctly
associated with the onclick attribute.

This is the code:

var imgNumber = 1;
var imgDot = document.createElement('div');

var onClickFunction = "ss_displayNthPicture("+imgNumber+")";
imgDot.setAttribute("onclick",onClickFunction);

document.body.appendChild(imgDot);
Any idea? Am I doing something wrong with quotes
and double quotes maybe? Or are there limitations
on the events a dynamically generated DIV can trigger?

Thanks for your help.

Manu
Jul 23 '06 #2
ma****@gmail.com said the following on 7/23/2006 9:49 AM:
Hi everybody,

I'm generating some DIVs dinamically, inclusive of
an event function to run when the user clicks on them.

While the following lines work perfectly in FF 1.5,
I.E. 6.0 ignores the onclick function alltogether:
the flow never gets into it, as if it hadn't been registered
properly. Yet, an inspection of the DOM with the
developer toolbar, reveals that the function is correctly
associated with the onclick attribute.

This is the code:

var imgNumber = 1;
var imgDot = document.createElement('div');

var onClickFunction = "ss_displayNthPicture("+imgNumber+")";
imgDot.setAttribute("onclick",onClickFunction);
setAttribute is known to be buggy in IE. So, instead of setAttribute,
simply access it directly:

imgDot.onclick = onClickFunction;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 23 '06 #3
Thanks for your help, Justin and Randy.

I had tried

imgDot.onclick = onClickFunction

but it didn't manage to coerce it to work.

attachEvent did work however, and I'm now using that method.

Again, thank you both.

Manu

Jul 23 '06 #4
ma****@gmail.com wrote:
Thanks for your help, Justin and Randy.
Please quote what you are replying to.

I had tried

imgDot.onclick = onClickFunction
Because onClickFunction should be a reference to a function, not a
string. Given that you originally tried:
var imgNumber = 1;
var imgDot = document.createElement('div');
var onClickFunction = "ss_displayNthPicture("+imgNumber+")";
imgDot.setAttribute("onclick",onClickFunction);

The fix is to use:

imgDot.onclick = function(){ss_displayNthPicture(imgNumber);}
If the above is inside some other function, then the reference back to
imgNumber creates a closure and hence may give an unexpected value for
imgNumber when ss_displayNthPicture() is called (or not). If that's a
problem, search recent posts to find a cure.

but it didn't manage to coerce it to work.

attachEvent did work however, and I'm now using that method.
What are you using for browsers that don't support IE's proprietary
attachEvent method?
--
Rob

Jul 24 '06 #5

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

Similar topics

8
by: Shock | last post by:
Hello everyone, I am having a problem with the program below. I have isolated the problem to the onclick event that is located throughout arrQuestions. The onclick event refers to a function...
3
by: Jamie Jackson | last post by:
I'm rewriting all links' onclick events, but I'm having a problem. The onclick event that I'm inserting works correctly in Opera, but not in FF or IE. I'm retroactively adding the statement...
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...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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.