An open API service indexing awesome lists of open source software.

https://github.com/codeadamca/javascript-mouse-events

A basic example of using JavaScript to connect mouse events to DOM changes.
https://github.com/codeadamca/javascript-mouse-events

javascript learning-code mouse-events

Last synced: 11 months ago
JSON representation

A basic example of using JavaScript to connect mouse events to DOM changes.

Awesome Lists containing this project

README

          

# A Basic Introduction to JavaScript and Mouse Events

Once you are comfortable with referencing different elements in the HTML document using the DOM (Document Object Model), the next step is to connect the manipulation of the elements in teh DOM to events. JavaScript has many events that can be connected to blocks of code:

1. onclick
2. ondblclick
3. onmousedown
4. onmousemove
5. onmouseout
6. onmouseover

## Example Event

If we had the following HTML document:

```html

The JavaScript DOM

The JavaScript DOM

Mauris convallis dictum odio. Quisque euismod finibus.

codeadam.ca

Click Me!

```

If we wanted to change the colour of the `h1` element when the `button` element is clicked, we would first create a reference to the button element:

```javascript
var button = document.getElementById('button');
```

And then add an event listener:

```javascript
button.addEventListener("click",function(){
document.getElementById("heading").styles.color = "red";
});
```

## Trying It Out

Create a new HTML document, add four `div` elements. Add the following four events and DOM manipulation:

1. Change the `background-colour` of the first `div` elements is clicked.
2. Change the `background-colour` of the second `div` element when the mouse enters.
3. Change the `background-colour` of the third `div` element when the mouse leaves.
4. Change the `background-colour` of the fourth `div` element when double clicked.

> Full tutorial URL:
> https://codeadam.ca/learning/javascript-mouse-events.html

***

## Repo Resources

* [Visual Studio Code](https://code.visualstudio.com/)