var j;
function myFunc(){
j = 0; // this is valid
}
j = 1; // this is also valid
function myFunc(){
var i; // variable i can be accessed only within the function
i = 0;
}
i = 1; // this is NOT valid
if (true) {
let message;
message = "Hello from inside the block!";
console.log(message); // This works and logs: "Hello from inside the block!"
}
console.log(message); // This will throw an error: "ReferenceError: message is not defined"
// Also, isn't let easier to read?
// let message = "Hello from inside the block!"
<!DOCTYPE html> <html lang="en"> <head> <title>Document demo</title> </head> <body> <p> This file loaded: </p> <div id="timestamp"> </div> </body> </html>
<script> var mytimestamp = document.getElementById("timestamp"); // PART1: Search for DOM mytimestamp.innerHTML = Date(); // PART2: Do stuff mytimestamp.style.backgroundColor = "yellow"; </script>
document.getElementById(); // returns a single element document.getElementsByTagName(); // returns an array document.getElementsByClassName(); // returns an array document.querySelector() // returns a single element document.querySelectorAll() // returns an node list
<img id="myImage" src="smiley.gif"> ... ... <script> var mysrc = document.getElementById("myImage"); mysrc.src = "sadface.jpg"; </script>
<img id="myImage" src="smiley.gif">
...
...
<script>
document.getElementById("myImage").src = "sadface.jpg";
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<title> The Title of this HTML Page </title>
<style>
body {
width: 100%;
height: 5000px;
}
</style>
<script>
function sliderchange(num){
//alert(num);
document.getElementById("mydiv").innerHTML = num;
document.getElementById("mydiv").style.color = "rgb("+num+", "+num+", "+num+")";
if(Number(num) > 220){
document.getElementById("mydiv").style.color = "black";
}
// lets turn this into a slider to move an image!
document.getElementById("colordiv").style.left = num + "px";
}
function resetslider(){
var sl = document.getElementById("slider");
sl.value = "0";
}
</script>
</head>
<body>
<br><br><br>
<input type="range" min="1" max="255" value="128" onchange="sliderchange(this.value);" style="width:50%;" id="slider" >
<br>
<button type="button" onclick="resetslider();">reset</button>
<div id="mydiv" style="font-size:20px;">
</div>
<!-- this could be an image -->
<div id="colordiv" style="width:20px; height: 20px; background-color:blue; position:absolute; top:200px; left:128px;"></div>
</body>
</html>