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

attaching event handlers to tags with javascript

P: n/a
Say I had <select id="something" name="something"
onchange="demo(this.value)"> but didn't want to define the onchange
event handler in the select tag, itself. ie. I wanted to attach it,
latter, by doing something like...

document.getElementById('something').attachEvent(' onchange',demo);

or this:

document.getElementById('something').onchange = demo;

How would I go about passing parameters to demo as I do in the select
tag? Is there a way I could reference this.value in demo without
having to pass it as a parameter to demo?

Dec 24 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
yawnmoth wrote:
Say I had <select id="something" name="something"
onchange="demo(this.value)"> but didn't want to define the onchange
event handler in the select tag, itself. ie. I wanted to attach it,
latter, by doing something like...

document.getElementById('something').attachEvent(' onchange',demo);
Don't forget to include a fork for addEventListener. Try this post by
Alexis Nikichine titled "IE's Dummy attachEvent?":

<URL:
http://groups.google.com.au/group/co...56d9d7b56f1efe
Copied below:

function makeEventFunc( param1, param2 )
{
return function()
{
alert( "param1 = " + param1 + "\nparam2 = " + param2 );
}

}

objElement. attachEvent ( "onclick", makeEventFunc( param1, param2 ) );

That way, makeEventFun will return you a closure, i.e. a function who
"remembers" what values were bound to param1 and param2. This
"function" can then be passed to attachEvent .

Hope this helps,

Alexis

or this:

document.getElementById('something').onchange = demo;
var x = document.getElementById('something');
x.onchange = function() {
demo(parm1, parm2);
}
'x' used for posting sans auto-wrapping.

How would I go about passing parameters to demo as I do in the select
tag? Is there a way I could reference this.value in demo without
having to pass it as a parameter to demo?


If you have:

document.getElementById('something').onchange = demo;
Then in demo() 'this' will refer to the element with id 'something'.
e.g.
window.onload = function() {
document.getElementById('something').onchange = demo;
}
function demo(){
alert(this.id); // shows 'something'
}


--
Rob
Dec 24 '05 #2

P: n/a

RobG wrote:
yawnmoth wrote:
Say I had <select id="something" name="something"
onchange="demo(this.value)"> but didn't want to define the onchange
event handler in the select tag, itself. ie. I wanted to attach it,
latter, by doing something like...

<snip>
function demo(){
alert(this.id); // shows 'something'
}


That helped - thanks! :)

Dec 24 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.