Adding interactivity
There is little use in programming unless you allow the user to actually do something — that is, to interact with the website.
That’s why responding to user events such as clicks, mouse overs and key strokes is almost always a starting point of any jQuery code. The best part? It’s dead simple. Here is an example to get you started:
Here is what happens above:
- The $(“#big-button”) part tells jQuery to select the element with id=”big-button”.
- The “.click “ tells jQuery we want to use click method and run showSomething function whenever user clicks the #big-button.
- Function showSomething() contains the code we actually want to run on each click. In this case, it’s just a standard JavaScript “alert” method which displays a message to the user.
Here is another example with slightly more code but fancier result. Can you guess what it does?
