Solved

Why Doesn't input.setAttribute("onblur", "myFunction(this)") Work in IE?

Posted on 2007-12-06
5
1,231 Views
Last Modified: 2013-11-19
I am dynamically creating elements and want to set onblur to do something, this works in FF but not IE,

input.setAttribute("onblur", "myFunction(this)")

So basically the node will look something like this in the end:

<input type="text" onblur="myFunction(this)">

Is there an easy fix?
0
Comment
Question by:dignified
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:kebabs
ID: 20418442
You have to use input.onblur unfortunately

IE interprets what you have as input[onblur] = "myFunction(this)" with the JS as a string.
0
 

Author Comment

by:dignified
ID: 20418459
That doesn't work using

input.onblur = "myFunction(this)"

This is going to be annoying then to try and get the "this" to perform properly. I may have to change the code to use a cloneNode(true)
0
 
LVL 8

Accepted Solution

by:
kebabs earned 125 total points
ID: 20418472
Yep, I think this is the closest you can get:

input.onblur = myFunction;

What is it you are trying to achieve with this scripting? I think a different approach is needed.
0
 
LVL 8

Expert Comment

by:kebabs
ID: 20418507
I didn't suggest addEventListener because it is not well supported but forgot I tackled this same issue earlier. If you are interested, here is the cross browser compatible function I use. It invokes addEventListener, attachEvent or a pseudo method I conjured up that I use for this sort of stuff. Should work in your case.

e.g.
addEvent('input', 'blur', 'myFunction');

function myFunction(evt) {
// ...

Also see:
http://www.captain.at/howto-addeventlistener-attachevent-parameters.php
function addEvent(ele, event, callback) {
  if (typeof(ele) == "string")
    ele = byId(ele);
  if (ele.addEventListener) {
    ele.addEventListener(event, callback, false);
  } else if (ele.attachEvent) {
    ele.attachEvent("on"+event, callback);
  } else {
    psuedoAttachEvent(ele, event, callback);
  ele["on"+event] = function() { psuedoTriggerEvents(ele, event) };
  }
}
 
function psuedoAttachEvent(ele, event, callback) {
  if (!ele.psuedoEvents) ele.psuedoEvents = {};
  if (!ele.psuedoEvents[event]) ele.psuedoEvents[event] = [];
  ele.psuedoEvents[event][ele.psuedoEvents[event].length] = callback;
}
 
function psuedoTriggerEvents(ele, event){
  if (ele && ele.psuedoEvents && ele.psuedoEvents[event])
    for (var i=0, len=ele.psuedoEvents[event].length; i<len; i++)
      ele.psuedoEvents[event][i]();
}

Open in new window

0
 

Author Comment

by:dignified
ID: 20418612
thanks a lot. Actually what I did was had a dummy template node with the onblur already in there and set it to display:none

and then I clone it and change the id to make it unique.

Thanks for your help
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question