Lesson 3: Buttons

1. Add a div into the gameWindow to use for the button. Give the div a meaningful id. Label the button Move because this div button will be used to move the animals.

2. Add CSS to style the div as a button. Place the styles that are for all buttons in .button, and place the unique styles that are for only btnMoveAnimals in #btnMoveAnimals.

Play around with the CSS to get your button to appear as you like.

Use border-radius.com to quickly generate the code needed to give your button rounded corners.

3. Make sure that the button style changes when you move the mouse over it. This is accomplished by applying a style for .button:hover.

4. One issue is that the user is able to select the text that is in the div by clicking and dragging over it.

To prevent this behavior, create a noSelect class and add that to btnMoveAnimals.



Notice that to assign multiple classes to one element, you just list them within a single class="" and separate each class name by a space.

Also, notice that 5 lines of code were used instead of one to apply the style. This is because there is currently not one property that all browsers recognize to do this. For instance, Firefox currently only recognizes -moz-user-select: none; and Chrome currently only recognizes -webkit-user-select: none;.

Browser support is always changing so you will often want to regularly check your sites on all major browsers.

Check out caniuse.com to see what HTML and CSS is, and is not, supported by various browsers.

5. Make the button do something. Add an onclick with the an alert() function.

Now a message should appear when the button is pressed.

6. This is a great way to test out your button. However, instead of calling the JavaScript alert() function, we want to call our own custom function.

Here, we are calling a function named move. We know it is a function because it is immediately followed by parenthesis. Sometimes information is provided in the parenthesis (as was the case with the alert function) and sometimes there is not, as is the case with the move function. Also, unlike alert, move is not a pre-defined function of JavaScript.

7. Let's create the move function. This function, and any other JavaScript we add, can be placed inside script tags.

Be careful as you type in the JavaScript code. Just like HTML and CSS, a missing character or misspelled word can make the code not work. However, unlike HTML and CSS, JavaScript is case sensitive. Function is not the same as function and move is not the same as Move.

In the next lesson you will add more to the move function so that every time the button is pressed (and the function is called) the animals will be placed at a random position.


Test Yourself!

1. What is the correct notation for creating a CSS style for an element with a given class?
#className{ }
.className{ }
*className{ }
className{ }
2. What is the correct notation for creating a CSS style for an element with a unique id?
#idName{ }
.idName{ }
*idName{ }
idName{ }
3. What is the correct notation for creating a CSS style for an element (e.g. body, div, h1)?
#elementName{ }
.elementName{ }
*elementName{ }
elementName{ }
4. Whats a good website to check on the most current browser support for specific JavaScript commands?
border-radius.com
caniuse.com
W3Schools.com
lynda.com
5. Which is a proper function definition?
Function move(){ }
function move(){ }
Function move{ }
function move{ }
6. border-radius.com is a website that generates the code needed to
give an element rounded corners
give an element a border
give an element a radius
set the thickness of the border of an element
7. What is not a typical technique used when creating a button?
Give the button rounded corners
Display a different cursor when the mouse is over the button
Change the location of the button on the page when it is clicked
Modify one or more of the styles of the button when the user hovers over the button
8. alert('CSUN Rocks your socks'); is a
function call to a built-in JavaScript function
function call to a custom made JavaScript function
function declaration of a built-in JavaScript function
function declaration of a custom made JavaScript function
9. function move(){ } is a
function call to a built-in JavaScript function
function call to a custom made JavaScript function
function declaration of a built-in JavaScript function
function declaration of a custom made JavaScript function
10. move(); is a
function call to a built-in JavaScript function
function call to a custom made JavaScript function
function declaration of a built-in JavaScript function
function declaration of a custom made JavaScript function
Practice with Alerts