473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

attaching event handlers to tags with javascript

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
2 5246
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Mark Bram | last post by:
Howdy All! If I write an image tag inside an anchor tag like this (for a rollover): <A HREF="#" onMouseOver="..." onMouseOut="..."> <IMG SRC="..."> </A> Does the mouse event generated on the...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
6
by: Tim Fooy | last post by:
Hi all, I have the following problem. In my page i have a large <div> with tags inside it that have event handlers on them (onclick etc.). When i run div.innerHTML = moreText + div.innerHTML,...
9
by: VK | last post by:
My original idea of two trains, however pictural it was, appeared to be wrong. The truth seems to be even more chaotic. IE implements its standard down-up model: any mouse event goes from the...
5
by: knocte | last post by:
Hello. I am a web developer very worried about "bloat code" and "languages mixture". So, since some time, I always try to avoid completely the use of javascript in XHTML/HTML files. This leads...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
17
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
2
by: hharry | last post by:
Hello All, In VS 2003, I was able to add event handlers in the InitializeComponent function. In VS 2005, I can no longer see the auto- generated code. I tried to add the event handler for a...
5
by: debrief | last post by:
Hello, I have been trying to hide objects on the onclick event and it works fine in Firefox however IE does not respond at all. Is there a way around this? I have the following in my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.