Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How to Add Event Handlers?
How to Add Event Handlers?
✍: Guest
You can add an event handler in the HTML definition of the element like this:
<script type="text/javascript"><!--
function hitme() {
alert("I've been hit!");
}
// -->
</script>
<input type="button" id="hitme" name="hitme" value="hit me" onclick="hitme()"
Or, interestingly enough you can just assign the event's name on the object directly
with a reference to the method you want to assign.
<input type="button" id="hitme2" name="hitme2" value="hit me2"/>
<script type="text/javascript"><!--
function hitme2() {
alert("I've been hit too!");
}
document.getElementById("hitme2").onclick = hitme2;
// -->
</script>
You can also use an anonymous method like this:
document.getElementById("hitme3").onclick = function () { alert("howdy!"); }
You can also use the the W3C addEvventListener() method, but it does not work in IE yet:
<input type="button" id="hitme4" name="hitme4" value="hit me4"/>
<script type="text/javascript"><!--
function hitme4() {
alert("I've been hit four!");
}
if(document.getElementById("hitme4").addEventListener) {
document.getElementById("hitme4").addEventListener("click", hitme4, false);
}
// -->
</script>
2011-05-24, 3669👍, 0💬
Popular Posts:
Where are all .NET Collection classes located ? System.Collection namespace has all the collection c...
What Happens If a Hyper Link Points to a Music File? - XHTML 1.0 Tutorials - Understanding Hyper Lin...
How To Compile a JUnit Test Class? Compiling a JUnit test class is like compiling any other Java cla...
How Can we change priority & what levels of priority are provided by Dot Net? Thread Priority ca...
How do we create DCOM object in VB6? Using the CreateObject method you can create a DCOM object. You...