473,545 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

onClick not working in IE

Nx
i've got it all working nicely in firefox, but whenever i test it in IE
none of the onclick events are triggered.

i'm using an xsl to transform an rss feed into a photogallery.

when i try to use setAttribute FF and safari work, but IE stops working
when i used addEventListene r and attachEvent safari stops working
and when i tried .onClick,none of them worked

being fairly inexperienced (but learning fast), i figure i'm doing it
wrong
any help is appreciated

snippet:

<![CDATA[for (var i = 0; i <= (the_news.categ ories.list.leng th-1);
i++){]]>
if(the_news.cat egories.list[i]!="News"){
tempCatDiv = document.create Element("div");
tempCatDiv.setA ttribute('class ','catitem');
tempCatDiv.setA ttribute('onCli ck','showItems( "'+the_news.cat egories.list[i]+'")');
tempCatDiv.inne rHTML = "· "+the_news.cate gories.list[i]+"";
catListDiv.appe ndChild(tempCat Div);
}
}

Mar 15 '06 #1
6 7157

Nx wrote:
tempCatDiv = document.create Element("div");
tempCatDiv.setA ttribute('class ','catitem');
tempCatDiv.setA ttribute('onCli ck','showItems( "'+the_news.cat egories.list[i]+'")');


IE's setAttribute method is far from perfect. Try setting those
properties directly:

tempCatDiv.clas s = "catitem";
tempCatDiv.oncl ick = function() {
showItems(the_n ews.categories. list[i]); };

Mar 15 '06 #2
Nx
thanks, i was formatting the onclick badly the first time, but fixing
it created another problem

tempCatDiv.oncl ick = function() {
showItems(the_n ews.categories. list[i]);
};

passes 'undefined' to showItems

can you give a clueless coder another freebie?

Mar 15 '06 #3
Nx said on 16/03/2006 7:00 AM AEST:
i've got it all working nicely in firefox, but whenever i test it in IE
none of the onclick events are triggered.

i'm using an xsl to transform an rss feed into a photogallery.

when i try to use setAttribute FF and safari work, but IE stops working
when i used addEventListene r and attachEvent safari stops working
and when i tried .onClick,none of them worked
JavaScript is case sensitive, the property you seek is 'onclick'.

being fairly inexperienced (but learning fast), i figure i'm doing it
wrong
any help is appreciated

snippet:

<![CDATA[
Are you really using XHTML? There seems very little point to it.
for (var i = 0; i <= (the_news.categ ories.list.leng th-1);
i++){]]>
Don't use tabs, use spaces for indenting code. Manually wrap your code
for posting at about 70 characters to allow for a coupe of quoted
replies before autowrapping takes over.

Why is just the opening - for - statement quoted XHTML-style? Use HTML
and ditch quoting inside script elements. With XHTML, use an external
..js file.

It is also more efficient to store values that are frequently referenced
in (local?) variables - the more dots used, the less efficient it becomes:

for (var i=0, j=the_news.cate gories.list.len gth; i<j; i++){

if(the_news.cat egories.list[i]!="News"){
Since the_news.catego ries.list[i] is used multiple times, create a local
variable that references it to save looking it up every time:

var newsItem = the_news.catego ries.list[i];
if ('News' != newsItem){
tempCatDiv = document.create Element("div");
tempCatDiv.setA ttribute('class ','catitem');
tempCatDiv.setA ttribute('onCli ck','showItems( "'+the_news.cat egories.list[i]+'")');
WebDev's suggestion here to use:

tempCatDiv.oncl ick = function() {
showItems(the_n ews.categories. list[i]);
};
creates another problem: the value of - the_news.catego ries.list[i] - is
evaluated when the onclick function executes. At that point, if
the_news.catego ries.list still exists as a global variable or closure to
a local variable, then the value of the_news.catego ries.list[i] will be
evaluated.

Since 'i' will also be either a global or closure to a local 'i' inside
whatever function is running here, it too will be evaluated when the
onclick occurs.

That is, the_news.catego ries.list[i] will be evaluated when the onclick
code is executed, not when the onclick function object was created.

At that time, (if it hasn't been modified elsewhere) the value of 'i'
will be what it was when the for loop finished. It will be the same as
the_news.catego ries.list.lengt h (whatever that was).

Since the value of the length attribute of a collection or array
(guessing that the_news.catego ries.list is one or the other) is always
at least one higher than the largest index, the_news.catego ries.list[i]
does not exist.

e.g. if there were 5 of them, the indexes range from 0 to 4. When the
for loop finishes, i=5 - all the onclick functions will try to access
the_news.catego ries.list[5] which doesn't exist.

The simple fix is, since the value is always 'News', to use that:

tempCatDiv.oncl ick = function(){ showItems('News '); };

Since the value 'News' is fixed (and essentially hard-coded in your
current function anyway), why not hard-code it in showItems() and then
you can have:

tempCatDiv.oncl ick = showItems; // Note no '()'
and showItems is now something like:

function showItems(){
var x = 'News';
...
}
Another, probably less efficient solution but more general, is to use
(with the substitution of newsItem):

tempCatDiv.oncl ick =
new Function('showI tems("'+ newsItem + '"];');

tempCatDiv.inne rHTML = "· "+the_news.cate gories.list[i]+"";
And:
tempCatDiv.inne rHTML = "· " + newsItem;
catListDiv.appe ndChild(tempCat Div);
}
}


--
Rob
Mar 16 '06 #4
RobG said on 16/03/2006 10:35 AM AEST:
Nx said on 16/03/2006 7:00 AM AEST: [...]
if(the_news.cat egories.list[i]!="News"){

Since the_news.catego ries.list[i] is used multiple times, create a local
variable that references it to save looking it up every time:

var newsItem = the_news.catego ries.list[i];
if ('News' != newsItem){

[...] The simple fix is, since the value is always 'News', to use that:

tempCatDiv.oncl ick = function(){ showItems('News '); };
Oops! Forgot the '!' the value of newsItem will *not* be 'News' - use
the suggestion below.
[...]
tempCatDiv.oncl ick =
new Function('showI tems("'+ newsItem + '"];');


--
Rob
Mar 16 '06 #5
web.dev wrote:
Nx wrote:
tempCatDiv = document.create Element("div");
tempCatDiv.setA ttribute('class ','catitem');
tempCatDiv.setA ttribute('onCli ck','showItems( "'+the_news.cat egories.list[i]+'")');
IE's setAttribute method is far from perfect.
Element::setAtt ribute() has many buggy implementations .
Try setting those properties directly:
Full ACK.
tempCatDiv.clas s = "catitem";


It is `className', which is well-known here. `class' is a reserved
word in several languages, including ECMAScript implementations .
PointedEars
Mar 17 '06 #6
Nx wrote:
thanks, i was formatting the onclick badly the first time, but fixing
it created another problem

tempCatDiv.oncl ick = function() {
showItems(the_n ews.categories. list[i]);
};

passes 'undefined' to showItems
If the_news.catego ries.list[i] evaluates to a different
value, it should not. The error must be elsewhere.
can you give a clueless coder another freebie?


You could RTFM for a change.
PointedEars
Mar 17 '06 #7

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

Similar topics

2
9148
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display in the status bar 'Symantec Corporation' whenever anyone mouses over (onMouseOver) my image or link OR when one clicks while holding the left mouse...
4
3644
by: masantha wee | last post by:
Hi all, I am using Firefox and embedding Javascript in html. I understand that we can use mouse events by coding them in the body of html (by creating a button or anything and by adding in the events in the <img> tag). <input id="StdDev Value" name="StdDevButton" type="button" value="Standard Deviation Value"...
1
9626
by: Chris Soulsby | last post by:
Hi, I have a control that contains a embedded ie control. I used the following code to add a onclick event to a table element: IHTMLDocument2 oIHTMLDocument2 = (IHTMLDocument2)m_oWebBrowserControl.Document; mshtml.HTMLTableClass oHTMLElement = (mshtml.HTMLTableClass)oIHTMLDocument2.all.item(strObjectName, null);...
5
2573
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...
0
1297
by: none | last post by:
Hi, I’ve got a problem with my onclick handlers for command buttons being dropped on page render for a popup window. On the opening window, I have several command buttons, and they all have the standard click handler references (as they should): -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- <input type="submit" name="button03"...
5
2073
by: Magician | last post by:
Hello. I am trying to set the onclick event for images through a function, but the event is triggered as soon the page loads, then will not work when the image is clicked. Can anyone suggest what is wrong? It is happening in both IE6 and Firefox --------------------------------------------------------------------------- window.onload =...
2
18555
by: =?Utf-8?B?TWFyaw==?= | last post by:
I am writing a control and want to handle an OnClick event on the client-side before then conforming to the postback mechanism. I have got the server-side events working for my OnClick event by adding to the rendered element's OnClick handler using GetPostBackEventReference. How can I provide a client-side onclick handler and then to use the...
4
7148
by: Aaron Gray | last post by:
The following input checkbox onclick via DOM is not working on IE7, FF, or WebKit, but is working on Opera for some strange reason. http://www.aarongray.org/Test/JavaScript/checkbox.html Here's the code :- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
1
4302
rahulephp
by: rahulephp | last post by:
CSS Tabs - contents of a div disappear in IE & onclick is not working Here is the code: Swaps the "active" tab and "inactive" tab and swaps out the new content via the display style attribute: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html...
0
7479
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7669
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. ...
1
7439
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...
0
7773
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...
0
5987
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...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
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...
1
1901
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
1
1028
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.