Setting an attribute in IE not working

O

ofirpicazo

Hi, my problem is that IE doesn't seem to do what it's supposed to, so
I hope u guys can give me a hand.
The code below is supposed to make text editable whenever it's clicked,
and it works, the problem is that IE doesn't set the onClick attribute
back to what I tell it to, at least it doesn't do anything if you click
on the text again, as opposed to what Firefox does, FF works!.
I put an alert on the onBlur event to see if IE sets the attribute back
and the weird thing is that it tells me it has done it, but it doesn't
work, it's weird, I hope u guys can help me out with this.
Thanks,

-Ofir

My code:
----------------
<html>
<head>
<title> New </title>

<script language="JavaScript" type="text/javascript">

function doEdit(it) {
var oldValue = it.innerHTML;
it.innerHTML = '<input type="text" value="'+oldValue+'"
onBlur="doUpdate(this.parentNode, this.value);">';
it.removeAttribute('onClick'); // For Mozilla
it.onclick = ''; // For IE
}

function doUpdate(field, value) {
field.innerHTML = value;
field.setAttribute('onClick', 'doEdit(this)'); // For Mozilla
field.onclick = 'doEdit(this)'; // For IE

alert(field.getAttribute('onClick'));
}
</script>
</head>

<body>
<span onClick="doEdit(this)" style="padding:5px;border:1px solid
Gray;">Edit this</span>
</body>
</html>
 
M

Michael Winter

On 18/10/2005 17:11, (e-mail address removed) wrote:

[snip]
<script language="JavaScript" type="text/javascript">

The language attribute is deprecated and redundant. Omit it.
function doEdit(it) {
[snip]

it.removeAttribute('onClick'); // For Mozilla
it.onclick = ''; // For IE

The onclick property is not a string; it's a function reference. Though
one includes the code in HTML as a string, this is internally converted
into a function by the browser. When the property is manipulated
directly, it is up to the author to perform this step.

Replace both lines with:

it.onclick = null;
function doUpdate(field, value) {
[snip]

field.setAttribute('onClick', 'doEdit(this)'); // For Mozilla
field.onclick = 'doEdit(this)'; // For IE

The same applies here. Both can be replaced by a single assignment:

field.onclick = function() {doEdit(this);};

However, to avoid a memory leak in IE that results from the use of the
closure above, the field variable should assigned null before the
doUpdate function returns.

field = null;

[snip]

Mike


Closures
<URL:http://www.jibbering.com/faq/faq_notes/closures.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top