By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Setting an attribute in IE not working

P: n/a
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>

Oct 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 18/10/2005 17:11, of********@gmail.com 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>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.