INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Event Handling

Change handlers on-the-fly by baad
Posted: 30 Aug 01

I see this kind of questions on these forums pretty often :
~how 2 add something to ... event handler
or
~how 2 remove some handler
áááááááááááááááááááá
i'll talk about onclick handler, but it could be any of them..
& another thing: this code works in ie only (at least onclick is not supported by nc4)

0. have a look at my faq about enabling/disabling "readonly" attribute of form fields: FAQ216-795 but there i just removed handler with
handler=new Function()

1. To remove defined handler just write
onclick=null or like i did it in the pointed faq

2. a bit harder: How to add to/remove something from event handler:

just copy-paste this code & play with it

<html>
<head>
<title>changing function code</title>
<script>

/*********************
the main monster: this function gets handler's body
*********************/

function getFuncBody(funcPtr) {
//handler's body
var str=funcPtr.toString();
// removes everything including first "{" (e.g. function name(){)
str=str.replace(/[^{]+{/,"");
// removes last }
str=str.substring(0,str.length-1);
// guess what :))
return str;
}

//onclick handler:
function clicker(){
one();one()
one()
}

//function that is used in onclick handler:
function one() {
var ii=0
if (!ii){alert('one')}
}


/*********************
function wich adds to current handler some actions: this time it is just alert('two') but u can call some other function.. play around
*********************/

function doIt() {
document.onclick=new Function(
getFuncBody(document.onclick)+";alert('two')" );
}




/*********************
function wich removes all 'one()' function calls, but u can do whatever u whant
*********************/

function doItagain() {
var str=getFuncBody(document.onclick)
str=str.replace(/one\(\)/g,';') //removal
document.onclick=new Function(str);
}

//setting onclick event handler
document.onclick= clicker;
</script>
</head>
<body bgcolor="#FFFFFF">
<input type=button onclick="doIt()" value=add><br>
<input type=button onclick="doItagain()" value=remove><br>
</body>
</html>


check out this thread to see the brainStorm: Thread216-128339
well, looks good

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close