Forum Moderators: open

Message Too Old, No Replies

Problem Creating Attributes in DIV

         

stuartc1

10:04 am on May 9, 2006 (gmt 0)

10+ Year Member



I'm creating a DIV using JavaScript. I need to put an event on the DIV so when an action like 'onClick' or 'event.keyCode == 13' has occured I call a javascript function. The DIV is contentEditable.

I've used various methods of adding the onClick to the DIV but IE6 refuses to accept it. All methods I've used work perfect in FF (which is no good because I have a contentEditable DIV which is not supported in FF).

Here is the first code I used:

var div = document.createElement('DIV');
div.contentEditable = true;
var attr = document.createAttribute('onClick');
attr.value = "alert('hello')";
div.setAttributeNode(attr);

Second Method:

var div = document.createElement('DIV');
div.contentEditable = true;
div.setAttribute("onClick", "alert('helo')");

Third Method:

var div = document.createElement('DIV');
div.contentEditable = true;
div.id = "myDiv";
document.getElementById('myDiv').setAttribute("onClick", "alert('helo')");

All of the above methods should create this:
<DIV contentEditable="true" onClick="alert('hello')"></DIV>

Any ideas?

stuartc1

10:27 am on May 9, 2006 (gmt 0)

10+ Year Member



Nevermind....

I solved it by adding a hardcoded DIV around the javascript generated one.