Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'this' not working in javascript function in IE

Status
Not open for further replies.

ranadhir

Programmer
Nov 4, 2003
54
0
0
IN
I have the following code in Mozilla:

<script type="application/x-javascript">
function listElementHandlers(aObj)
{
if(!aObj)
return null;
for(var list in aObj)
if(list.match(/^on/))
alert(list+'\n');
}
function dummy()
{
}
<script>

..
<input type="text" id="searchField" on keyup="listElementHandlers(this)" on blur="dummy()" >
..
This lists out all the 'on' events against that element(onkeyup and onblus in ths icase).
This does not work in IE- is it because 'this' in IE represents the document object?
How then do we simulate this in IE?
 
Only way to get it going on ie is to change the type to the more orthodox type="text/javascript" if you don't mind.
 
Please make this code work on mozilla and IE - in mozilla you will only see onkeyup and onblur which are the events defined for the owning element searchField - but in IE it will print out a host of events not related to the 'searchField' element at all.
In fact,I guess in IE it starts printing out all events at the document level - which is not what I intend
 
[1] You said it works on moz. But the handlers are named there without "on" prefix? (Verify through the script below yourself. Un-comment the if () { } testing the on prefix.)
[2] Make it to onkeyup is very annoying. I, for demo purpose, put it in a button and list input textbox element as you originally intended.
[tt]
<html>
<head>
<script type="text/javascript">
function listElementHandlers(obj) {
var s="";
for(var list in obj) {
[blue]//if (/^on/.test(list)) {[/blue]
s+=list+"<br />"
[blue]//}[/blue]
}
document.getElementById("divid").innerHTML=s;
}
</script>
</head>
<body>
<!-- very annoying if you use onkeyup to do that listing -->
<form name="formname">
<input type="text" id="searchField" name="searchField" /><br />
</form>
<button onclick="listElementHandlers(document.formname.searchField)">get the list</button><br />
<div id="divid"></div>
</body>
</html>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top