Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an event object to a function in Javascript?

<button type="button" value="click me" onclick="check_me();" />  function check_me() {   //event.preventDefault();   var hello = document.myForm.username.value;   var err = '';    if(hello == '' || hello == null) {     err = 'User name required';   }    if(err != '') {       alert(err);       $('username').focus();       return false;     } else {      return true; } } 

In Firefox, when I try to submit an empty value it throws up the error and sets the focus back to element. But same thing doesn't happen in IE as it throws up error and after clicking OK and posts the form (returns true).

How I can avoid this? I was thinking to avoid this using event.preventDefault(), but I am not sure how to do this using this method. I tried passing checkme(event) .. but it didn't work. I am using Prototype js.

(I know how to pass an event when I bind an .click function in Javascript.. instead of calling onclick within html .. using Jquery, but I have to debug this piece of code)

like image 311
TigerTiger Avatar asked Aug 14 '09 09:08

TigerTiger


People also ask

Can you pass an object to a function in JavaScript?

To pass an object to a JavaScript function, we can add a parameter that accepts an object. const someFunc = (arg) => { alert(arg. foo); alert(arg. bar); }; someFunc({ foo: "This", bar: "works!" });

How do you pass an element to a onClick function?

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that's called with this . to set the onclick attribute to the a elements to call onClick with this .

Can we pass class objects as function arguments in JavaScript?

In JavaScript, you can use functions as values, just like numbers, strings, and objects. That means you can pass them as arguments, return them from other functions, and set them as properties of objects.

What is the event object in JavaScript?

When a W3C event listener's event occurs and it calls its associated function, it also passes a single argument to the function—a reference to the event object. The event object contains a number of properties that describe the event that occurred.


1 Answers

  1. Modify the definition of the function check_me as::

     function check_me(ev) { 
  2. Now you can access the methods and parameters of the event, in your case:

     ev.preventDefault(); 
  3. Then, you have to pass the parameter on the onclick in the inline call::

     <button type="button" onclick="check_me(event);">Click Me!</button> 

A useful link to understand this.


Full example:

<!DOCTYPE html> <html lang="en">   <head>     <script type="text/javascript">       function check_me(ev) {         ev.preventDefault();         alert("Hello World!")       }     </script>   </head>   <body>     <button type="button" onclick="check_me(event);">Click Me!</button>   </body> </html> 









Alternatives (best practices):

Although the above is the direct answer to the question (passing an event object to an inline event), there are other ways of handling events that keep the logic separated from the presentation

A. Using addEventListener:

<!DOCTYPE html> <html lang="en">   <head>   </head>   <body>     <button id='my_button' type="button">Click Me!</button>      <!-- put the javascript at the end to guarantee that the DOM is ready to use-->     <script type="text/javascript">       function check_me(ev) {         ev.preventDefault();         alert("Hello World!")       }              <!-- add the event to the button identified #my_button -->       document.getElementById("my_button").addEventListener("click", check_me);     </script>   </body> </html> 

B. Isolating Javascript:

Both of the above solutions are fine for a small project, or a hackish quick and dirty solution, but for bigger projects, it is better to keep the HTML separated from the Javascript.

Just put this two files in the same folder:

  • example.html:
<!DOCTYPE html> <html lang="en">   <head>   </head>   <body>     <button id='my_button' type="button">Click Me!</button>      <!-- put the javascript at the end to guarantee that the DOM is ready to use-->     <script type="text/javascript" src="example.js"></script>   </body> </html> 
  • example.js:
function check_me(ev) {     ev.preventDefault();     alert("Hello World!") } document.getElementById("my_button").addEventListener("click", check_me); 
like image 76
toto_tico Avatar answered Oct 12 '22 08:10

toto_tico