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, 3709👍, 0💬
Popular Posts:
How To Enter Numeric Values as HEX Numbers? - MySQL FAQs - Introduction to SQL Basics If you want to...
What are the five levels in CMMI? There are five levels of the CMM. According to the SEI, Level 1 – ...
What is the significance of Finalize method in .NET? .NET Garbage collector does almost all clean up...
What does a special set of tags <?= and ?> do in a PHP script page? <?= express...
Can you tell me how to check whether a linked list is circular? Create two pointers, and set both to...