javascript - How do you check whether a check box is checked or not -
this question has answer here:
- check if checkbox checked javascript 9 answers
i'm making points application. how check whether or not check box checked , if how add 10, if not checked want nothing. here javascript. js
if (localstorage.getitem("studentlist") === null) { localstorage.setitem("studentlist", json.stringify([])); } studentlist = json.parse(localstorage.getitem("studentlist")); function save() { newstudent = { "firstname": document.getelementbyid('sfirstname').value, "lastname": document.getelementbyid('slastname').value, "studentnumber": document.getelementbyid('snumber').value, "points": document.getelementbyid('spoints').value }; studentlist.push(newstudent); localstorage.setitem("studentlist", json.stringify(studentlist)); }; table = document.getelementbyid("attendance"); (i = 0; < studentlist.length; i++) { row = table.insertrow(i + 1); cell1 = row.insertcell(0); cell2 = row.insertcell(1); cell3 = row.insertcell(2); cell4 = row.insertcell(3); cell5 = row.insertcell(4); cell1.innerhtml = studentlist[i].firstname; cell2.innerhtml = studentlist[i].lastname; cell3.innerhtml = studentlist[i].studentnumber; cell4.innerhtml = studentlist[i].points; cell5.innerhtml = "<input type=checkbox>"; } function username() { //this function takes values text box , stores them in objects var studentlist = { studentnumber: document.getelementbyid("newuser").value + index, lastname: document.getelementbyid("newlname").value + index, firstname: document.getelementbyid("newfname").value + index }; }
and here's jsfiddle: https://jsfiddle.net/randomrainbow7/lm574foy/ fiddle won't show person if want hardcode in otherwise heres other html page.
<!doctype html> <head> <!--<link href="style.css" rel="stylesheet" type="text/css">--> <script src="function.js"></script> </head> <header> <nav> <img src="logo.jpg"><h1 id="titt">north park clubs</h1> <li> <a href="attendance.html">attendance</a> <a href="addstudent.html">add student</a> </li> </nav> </header> <body> <h1>add student</h1> <input type="text" id="sfirstname" placeholder="first name"></input> <input type="text" id="slastname" placeholder="last name"></input> <input type="text" id="snumber" placeholder="student number"></input> <input type="text" id="spoints" placeholder="points"></input> <input type="button" id="checkbox"onclick="save()" value="save"></input> </body>
you can store informatation of 'being absent' along other information of student in json object:
store in save()
function:
newstudent = { "firstname": document.getelementbyid('sfirstname').value, "lastname": document.getelementbyid('slastname').value, "studentnumber": document.getelementbyid('snumber').value, "points": document.getelementbyid('spoints').value, "absent": document.getelementbyid('sabsent').checked };
read in for-loop
table:
cell1.innerhtml = studentlist[i].firstname; cell2.innerhtml = studentlist[i].lastname; cell3.innerhtml = studentlist[i].studentnumber; cell4.innerhtml = studentlist[i].points; cell5.innerhtml = "<input type=checkbox" + (studentlist[i].absent === true) ? "checked>" : ">"; }
write in html under <h1>add student</h1>
:
<input type="checkbox" id="sabsent"></input>
Comments
Post a Comment