473,405 Members | 2,444 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,405 software developers and data experts.

IE setAttribute onclick

107 100+
yea i know there is millions of stuff about this on the net, and this isnt really a question but just wanted to see if people had picked up anything similar.

This is only for internet explorer, firefox doesnt have any problem.

Expand|Select|Wrap|Line Numbers
  1. i = 666;
  2.  
  3.  
  4. myImg = document.createElement("img");
  5. myImg.setAttribute('onClick', 'doSomething(\'' +i+ '\');'); //this works
  6.  
  7. myDiv = document.createElement("div");
  8. myDiv.setAttribute('onClick', 'doSomething(\'' +i+ '\');');  //this doesn't
  9.  

so setAttribute for the onclick event works but not for Divs.

Anybody know why? Wouldnt this take extra coding to stop the function working?

this is the work around ive used anyway; after checking if the user is on IE:

Expand|Select|Wrap|Line Numbers
  1. myDiv.onclick = new Function( "return dosomething('" + i + "');" );

god i hate IE.
Jul 15 '07 #1
1 3221
acoder
16,027 Expert Mod 8TB
Use addEventListener (W3C standard). Of course, this won't work in IE, so use its own proprietary attachEvent method. See this link.

Also, check out this article where volectricity has normalized the two forms.
Jul 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Tim Johnson | last post by:
Hello All: Using javascript to dynamically add row elements to a table. as in ..... row.setAttribute("bgcolor",rowColor); // or cell.setAttribute("bgcolor",rowColor); Using firefox or...
5
by: johnsuth | last post by:
I want to produce a trivial demonstration of dynamic modification. I thought that pressing a button might change its color. I studied O'Reillys books and successfully created the button with a...
21
by: James Black | last post by:
I am curious if there is a benefit to set attributes directly, in my javascript, or to use setAttribute. For example, I have this: var input = document.createElementNS(xhtmlNS, 'input');...
5
by: gkelly | last post by:
Can someone explain what I am doing wrong, or why this will not work? I've tested this in IE6, Firefox 1.5 and Mozilla 1.7, all with the same result. Take for example this code: <html>...
11
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
13
by: Sebarry | last post by:
Hi, I have a form with an add button that when clicked should create a new textarea with edit and delete buttons and append it to the form. Both the edit and delete buttons need to respond to...
14
by: Sri02 | last post by:
All, I was trying to add an onclick event using setAttribute to a <tdfrom javascript. Apparently the code doesnot seem to work in FF3 but works pretty well in IE7. Here is the snippet for ur...
0
BarryA
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...
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
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
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...
0
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,...

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.