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 addEventListener 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.categories.list.length-1);
i++){]]>
if(the_news.categories.list[i]!="News"){
tempCatDiv = document.createElement("div");
tempCatDiv.setAttribute('class','catitem');
tempCatDiv.setAttribute('onClick','showItems("'+th e_news.categories.list[i]+'")');
tempCatDiv.innerHTML = "· "+the_news.categories.list[i]+"";
catListDiv.appendChild(tempCatDiv);
}
} 6 7136
Nx wrote: tempCatDiv = document.createElement("div"); tempCatDiv.setAttribute('class','catitem'); tempCatDiv.setAttribute('onClick','showItems("'+th e_news.categories.list[i]+'")');
IE's setAttribute method is far from perfect. Try setting those
properties directly:
tempCatDiv.class = "catitem";
tempCatDiv.onclick = function() {
showItems(the_news.categories.list[i]); };
thanks, i was formatting the onclick badly the first time, but fixing
it created another problem
tempCatDiv.onclick = function() {
showItems(the_news.categories.list[i]);
};
passes 'undefined' to showItems
can you give a clueless coder another freebie?
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 addEventListener 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.categories.list.length-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.categories.list.length; i<j; i++){
if(the_news.categories.list[i]!="News"){
Since the_news.categories.list[i] is used multiple times, create a local
variable that references it to save looking it up every time:
var newsItem = the_news.categories.list[i];
if ('News' != newsItem){
tempCatDiv = document.createElement("div"); tempCatDiv.setAttribute('class','catitem'); tempCatDiv.setAttribute('onClick','showItems("'+th e_news.categories.list[i]+'")');
WebDev's suggestion here to use:
tempCatDiv.onclick = function() {
showItems(the_news.categories.list[i]);
};
creates another problem: the value of - the_news.categories.list[i] - is
evaluated when the onclick function executes. At that point, if
the_news.categories.list still exists as a global variable or closure to
a local variable, then the value of the_news.categories.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.categories.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.categories.list.length (whatever that was).
Since the value of the length attribute of a collection or array
(guessing that the_news.categories.list is one or the other) is always
at least one higher than the largest index, the_news.categories.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.categories.list[5] which doesn't exist.
The simple fix is, since the value is always 'News', to use that:
tempCatDiv.onclick = 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.onclick = 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.onclick =
new Function('showItems("'+ newsItem + '"];');
tempCatDiv.innerHTML = "· "+the_news.categories.list[i]+"";
And:
tempCatDiv.innerHTML = "· " + newsItem;
catListDiv.appendChild(tempCatDiv); } }
--
Rob
RobG said on 16/03/2006 10:35 AM AEST: Nx said on 16/03/2006 7:00 AM AEST:
[...] if(the_news.categories.list[i]!="News"){
Since the_news.categories.list[i] is used multiple times, create a local variable that references it to save looking it up every time:
var newsItem = the_news.categories.list[i]; if ('News' != newsItem){
[...] The simple fix is, since the value is always 'News', to use that:
tempCatDiv.onclick = function(){ showItems('News'); };
Oops! Forgot the '!' the value of newsItem will *not* be 'News' - use
the suggestion below.
[...] tempCatDiv.onclick = new Function('showItems("'+ newsItem + '"];');
--
Rob
web.dev wrote: Nx wrote: tempCatDiv = document.createElement("div"); tempCatDiv.setAttribute('class','catitem');
tempCatDiv.setAttribute('onClick','showItems("'+th e_news.categories.list[i]+'")'); IE's setAttribute method is far from perfect.
Element::setAttribute() has many buggy implementations.
Try setting those properties directly:
Full ACK.
tempCatDiv.class = "catitem";
It is `className', which is well-known here. `class' is a reserved
word in several languages, including ECMAScript implementations.
PointedEars
Nx wrote: thanks, i was formatting the onclick badly the first time, but fixing it created another problem
tempCatDiv.onclick = function() { showItems(the_news.categories.list[i]); };
passes 'undefined' to showItems
If the_news.categories.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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: ...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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,...
|
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...
| |