Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does event binding mean?

What does event binding mean? I always come across this word whenever I search around the internet and whatever I try to look for the meaning, it's still vague to me @_@ A while ago, while reading some blogs regarding JavaScript I see people using this sacred word that I cannot grasp.

like image 217
Rei Avatar asked Jun 13 '11 11:06

Rei


People also ask

What is event binding in JS?

The event binding allows you to add an event handler for a specified event so that your chosen JavaScript function will be invoked when that event is triggered for the associated DOM element. This can be used to bind to any event, such as keypress , mouseover or mouseout .

What does a event binding use?

In Angular 8, event binding is used to handle the events raised by the user actions like button click, mouse movement, keystrokes, etc. When the DOM event happens at an element(e.g. click, keydown, keyup), it calls the specified method in the particular component.

Why is event binding in react?

Binding with an Arrow Function increaseCount will never change, and the button won't get rendered. To bind event handlers in React, the simplest, and the efficient way is to bind with arrow functions. Since this a new feature, it is not uncommon to see a lot of code using the binding in the constructor method.

What is event binding and property binding?

Property binding is the same thing as interpolation, except you can set the properties and attributes of various HTML elements. Event binding allows you to define events that occur in the template (user-initiated), and communicate to the component class.


3 Answers

Event binding refers to telling the browser that a particular function should be called whenever some 'event' occurs. Events mostly relate to user input, such as clicks.

An example of binding to an event in jQuery can be the following:

$("#elem").bind("click", function() {
    alert("Clicked!");
});

This binds a function to click event of DOM object with identifier elem. When user clicks it, an alert (message box) will be shown. Binding is done by invoking the jQuery bind function but there are other means to do that, (e.g. jQuery click function in case of binding to click event).

like image 147
Xion Avatar answered Oct 04 '22 12:10

Xion


When you bind something to an event, it will be triggered when the event is fired. It's like gluing a fog horn to the brake pedal on your car.

like image 25
Tak Avatar answered Oct 04 '22 12:10

Tak


When you perform an action on a web page, it will trigger an event. This might be something like:

  • Click a button
  • Select a value from a drop down
  • Hover the mouse over an item

These events can be captured in your JavaScript code.

A common (and often misguided) way of capturing events is to do so on the HTML element itself (as shown in the onclick attribute below)

<input id="MyButton" type="button" value="clickme" onclick="Somefunction()" />

So, when the user clicks the button, the SomeFunction function will be executed.

However, it is considered a better approach to adopt a technique called 'late-binding'. This ensures that your HTML and JavaScript are kept completely separate.

So, we can modify the above exmample like so:

document.getElementById("MyButton").onclick = function(){
   //functionality here.
}

jQuery makes this even easier:

$("#MyButton").click(function(){
    //functionality here.
});
like image 23
James Wiseman Avatar answered Oct 04 '22 11:10

James Wiseman