HTML
Mass: <input id ="massInput" type ="number" placeholder="mass">
Area: <input id ="areaInput" type="number" placeholder="Area">
</br>
<button onclick="getInput()">Calculate</button>
<p id = "result"</p>
JS
function getInput(){
let mass = parseInt(massInput.value);
let area = parseInt(areaInput.value);
//check that input is a number - if not provide message
if(!Number.isNaN(mass)){
console.log(mass);
}
else{
console.log("Mass not a number")
}
if(!Number.isNaN(area)){
console.log(area);
}
else{
console.log("Mass not a number")
}
//output a result in the paragraph id called result using innerHTML
result.innerHTML="The mass is " + mass + " and the area is " + area;
}
HTML - create a text box with id ans. Run function getResponse() on key press
<input id = "ans" type="text" size="40" onkeypress="getresponse(event)">
JS
//create a function that receives an argument called event
function getResponse(event){
if (event.keyCode == 13) { // Number 13 stores the "Enter" key on the keyboard
var ua = input.value; //store the value in text box into a variable
if (parseInt(ua)==answer){
feedback.innerHTML="Well Done";
score+=1;
}
else{
feedback.innerHTML="That was incorrect. The correct answer was " + answer;
}
input.value = ""; //reset text box to blank
core.innerHTML = "score: " + score; //update the score output
}
}
var countdown = 30; //create a counter variable to track seconds
countdown_output.innerHTML = countdown; //updates the HTML time area to display the start time 30 secs
var timer = setInterval(clock, 1000) // attach timer function to a variable. Runs clock every sec.
//function triggered every second - see above line
function clock(){
if(countdown>0){
countdown = countdown -1;
countdown_output.innerHTML = countdown; //updates the HTML element with new number
}
else{
clearTimeout(timer); //removes the timer
}
}
//generate a random item from an array
var list =["a","b","c"]; //create a list of items
console.log(list[Math.round(Math.random()*3)]) // round off the random number to access the index of the array
var list =[
{id:1,name:"fred"},
{id:2,name:"ted"},
{id:3,name:"peg"},
];
//Below is an example of sorting the above object list by name.
list.sort((a, b) => (a.name > b.name) ? 1 : -1)
console.log(list);
Please note that all names need to be either lower or upper case as this will impact the alphabetical sort algorithm
var list =[
{id:1,name:"fred"},
{id:2,name:"ted"},
{id:3,name:"peg"},
];
add();
function add(){
list.push({id:1,name:"Sam"});
}
function resultTable() {
var row = results_table.insertRow(-1); //add a new row to end of table
row.insertCell(0).innerHTML = "value"; //add new value to cell 1
row.insertCell(1).innerHTML = "value"; //add new value to cell 2
}
In the creation of the new table row - add a button and onclick listener in each row that says delete
<input type="button" value="Delete" onclick="deleteRow(this)">
Make sure you have global variable reference to your table
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
table_name.deleteRow(i);
}
table_name.deleteRow(2);
or if you created a const referencing the table at the top of your JS Note: -1 will delete the last row
table_name.deleteRow(-1);
If you know the table name, row and cell column, you can directly access a unique cell in the table. Replace the index with the row and cell you wish to edit. Remember the very first row (header) is index 0 and the first cell on the left hand side starts at 1.
table_name.rows[3].cells[2].innerHTML="value"
//This will edit LN3
In your css create a new css class.
.underline{
text-decoration:underline;
}
To edit a specific element and apply the class style to it
element_name.classList.add("underline");
To remove a specific elements class style:
element_name.classList.remove("underline");
Hide Element with space taken up
element_name.style.visibility= "hidden";
Show Element
element_name.style.visibility="visible";
To not have any space taken up when an element is hidden
element_name.style.display ="none";
To show the element again use display:block
element_name.style.display ="block";