JavaScript Flashcards
How would you add a simple button to change the text in the following:
<div>CHANGE THIS</div>
Click
The HTML below displays an image, update this so that when you click the image it runs a function called YouClickedMe()
<img></img>
<img></img>
Write the myFunction() for the button below which which will change the text to green:
<p>JavaScript can change the style of an HTML element.</p>
Click Me!
function myFunction() { var x = document.getElementById("demo"); x.style.color = "green"; }
IN JS, how do you find an element on a page by it’s Id?
Use:
document.getElementById(“[elementsId]”)
How would you get a window alert to pop up whenever someone loads a page?
Include something along the lines of:
window.alert(“HELLO”);
Complete the following:
JavaScript has ??????? types
JavaScript has DYNAMIC types
ie a var x can be assigned a number and then later a string
How many number types does JavaScript have?
Just one
set up a JavaScript array containing the following words:
Cat
Dog
Rabbit
var animals = [“Cat”, “Dog”, “Rabbit”];
Setup a javaScript object for the following person:
firstName = John,
lastName = Fentiman,
Age = 37
var john = {firstName:”John”, lastName:”Fentiman”, age:37};
How do you find the type of a variable?
var x = "John"; typeof x;
How do you clear out a variable?
var person = "John"; //This would set it up as a string person = undefined; //This would clear it out
Add a function to this object to return the full name:
var person = { firstName: "John", lastName : "Doe", id : 5566, } };
document.getElementById(“demo”).innerHTML = person.fullName();
var person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } };
document.getElementById(“demo”).innerHTML = person.fullName();
How would you find the 3rd character in the following:
var str = “HELLO WORLD”;
str.charAt(2);
DO NOT USE str[2]
This is not compatiable
How do you convert the value on an innerHTML to a number?
var i = parseInt(document.getElementById(“IdName”).innerHTML);
Create a JavaScript object call Person which includes a name and an age attribute along with a sub object called favouritePerson
var person = { name: "John", age: 37, favouritePerson: { name: "Gemma", age: 38 } };