To work with JavaScript, first, you need to add an identifier to the element that you want to add to the event. This is because it simplifies the understanding of your code and avoids confusion when writing code. So the HTML line will look like this:
<input type="text" name="text" id="myInputType1" />
For each element that is unique throughout the document, there should be no more than one identifier. Now there are three main ways to add events:
document.getElementById("myInputType1").onclick = function(){ }; function Func(){ } document.getElementById("myInputType1").onclick = Func; function Func(){ } document.getElementById("myInputType1").addEventListener("click", Func, false);
The advantage of the latter is that you can add as many click events (or mouseover, ...) as you want, and deleting them one by one is possible. But it does not work with IE <9. Instead, you should use:
document.getElementById("myInputType1").attachEvent("onclick", Func);
jQuery:
$("#myInputType1").click(function(){ });
Jesufer vn
source share