Forum Moderators: open

Message Too Old, No Replies

Attribute Changing

using javascript to change tag attributes

         

tforcram

2:23 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



I'm trying to change the functionality of a form I have. If the user inputs the correct value, I then want to change the onFocus attribute to stop any more changes to the value. Here is what I have:

<input id="1a0" type="text" onFocus="select()">

function checkAnswer{
if(document.forms[0].elements[i].value == answer){
document.forms[0].elements[i].setAttribute("onFocus", "this.blur()");
}
}

This works great for netscape, but I was wondering what will work in IE. I've tried [document.forms[0].elements[i].onfocus="this.blur()"] and when I check the attribute, it says it has changed, but the functionality doesn't. Anyone have any ideas?

Thanks.

Bernard Marx

2:28 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's kinda supposed to work, but it doesn't. It does actually change IE's "outerHTML" of the element, but won't effect its behaviour. Part of the reason is that onfocus isn't a 'normal' attribute, it is an event handler. This shouldn't matter, but IE seems to care.

The best thing to do is to to change it as a JS property, in much the same way as you were doing (but the correct one :) ). This will work cross-browser:

document.forms[0].elements[i].onfocus = function(){this.blur()}

tforcram

2:42 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



It works great, thanks for the help.

Hester

3:43 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input id="1a0" type="text" onFocus="select()">

Sadly IDs can't start with a number. See: [w3.org ]