0002 added[Accept userinput, typerconversion]
This commit is contained in:
@@ -11,6 +11,10 @@
|
|||||||
<p id="p1"></p>
|
<p id="p1"></p>
|
||||||
<p id="p2"></p>
|
<p id="p2"></p>
|
||||||
<p id="p3"></p>
|
<p id="p3"></p>
|
||||||
|
<!--Accept User Input at html document-->
|
||||||
|
<label id="myLabel">Enter your name</label><br>
|
||||||
|
<input type="text" id="myText"><br>
|
||||||
|
<button type="button" id="myButton">submit</button>
|
||||||
<script src="/js/index.js"></script>
|
<script src="/js/index.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
37
js/index.js
37
js/index.js
@@ -111,4 +111,39 @@ console.log(students);
|
|||||||
//--------------------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
//8. Accept user input
|
//8. Accept user input
|
||||||
//https://youtu.be/8dWL3wF_OMw?t=1288
|
//8.1 Easy way via prompt. Not very practical. Care that user input is at string datatype,
|
||||||
|
//so typeconversion may be needed.
|
||||||
|
//let username = window.prompt("What's your name?");
|
||||||
|
//console.log(username);
|
||||||
|
//8.2
|
||||||
|
let username;
|
||||||
|
document.getElementById("myButton").onclick = function() {
|
||||||
|
username = document.getElementById("myText").value;
|
||||||
|
console.log(username);
|
||||||
|
document.getElementById("myLabel").innerHTML = username;
|
||||||
|
}
|
||||||
|
//--------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
//9. Typeconversion
|
||||||
|
//Change the datatype of a value to another. (strings, numbers, boolean)
|
||||||
|
let age3 = "23";
|
||||||
|
console.log(typeof age3);
|
||||||
|
age3 = Number(age3);
|
||||||
|
age3++;
|
||||||
|
console.log(age3);
|
||||||
|
console.log(typeof age3);
|
||||||
|
|
||||||
|
let y = String(3.14);
|
||||||
|
console.log(y);
|
||||||
|
console.log(typeof y);
|
||||||
|
|
||||||
|
//Useful to check if user entered some required input.
|
||||||
|
let z = Boolean(""); //empty string is converted to false
|
||||||
|
console.log(z);
|
||||||
|
console.log(typeof z);
|
||||||
|
z = Boolean("z"); //not empty string is converted to true
|
||||||
|
console.log(z);
|
||||||
|
console.log(typeof z);
|
||||||
|
//--------------------------------------------------------------------------------------------------------------
|
||||||
|
//10. const keyword
|
||||||
|
//https://youtu.be/8dWL3wF_OMw?t=1812
|
||||||
Reference in New Issue
Block a user