Madumitha Balaji
Madumitha Balaji

Reputation: 19

Uncaught SyntaxError: Unexpected string in my Javascript code

I keep getting this error in my code and I don't understand why. See line 29 of my Javascript.

HTML

<link href="Styles12.css"; type="text/css" rel="stylesheet">
<link rel="stylesheet" href="Styles12.css"/>
<script src="registration.js"></script>
<body onload="studentAttendance()">
    <head>
        <style>
           table, td, th {
               border: 1px solid black;
           }
            th {
                background-color: beige;
                color:black;
            }
        </style>
    </head>
<h3 style= "font-size:25px; font-family:Impact"> ADD A STUDENT</h3>

Firstname:

Lastname:

Student number:

    <button onclick = "save()"><p>+ADD TO ATTENDANCE</a></p> </button>
    <h2 style="font-size: 25px;font-family: Impact">Online attendance</h2>    
    <table id= "attendanceTable">
        <table border="10px">
            <thead>
                <tr>
                    <th> Student Name </th>
                    <th> Student Number </th>
                    <th> A </th>
                    <th> Points </th>
                    <th style="font-size:10px;font-align: left"><em><button> Edit:Add student(+)</a></button></em></th>
                    </th>
                 </tr>
            </thead>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td><form action="demo_form.asp" method="get"></form></td>
            <td></td>
           <td> <input type="button" value="save"></input>
        <button>reset</button></td>
        </tr>
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td><form action="demo_form.asp" method="get"></form></td>
                <td></td>
            </tr>
            <tbody>
                <tr>
                    <td></td>
                    <td></td>
                    <td><form action="demo_form.asp" method="get"></form></td>
                    <td></td>
                </tr>
            </tbody>
        </table>

    <form>
    </form> 
    <button type="button" onlick="alert('Attendence submitted')">  <strong>SUBMIT</strong></button>
    <p id="demo"></p>
</body>

JAVASCRIPT

var studentNo = [];
var index = 0;
var studentInfo = [];
var newObj = [];

function save() { //this function takes values from the text box and stores them as an object studentInfo[index] = { studentNumber: document.getElementById("studentNo").value, firstname: document.getElementById("firstname").value, lastname: document.getElementById("lastname").value, }; index++;

    localStorage.setItem("studentRecord", JSON.stringify(studentInfo));
}

function studentAttendance() {
    newObj = JSON.parse(localStorage.getItem("studentRecord"));
    var table, row, cell1, cell2, cell3;
    table = document.getElementById("onlineAttendance");
    studentInfo = JSON.parse(localStorage.getItem("studentRecord"));
    for (var index = 0; index < studentInfo.length; index++) {
        row = table.insertRow(index + 1);
        cell1 = row.insertCell(0);
        cell2 = row.insertCell(1);
        cell3 = row.insertCell(2);
        cell4 = row.insertCell(3);
        cell1.innerHTML = studentInfo[index].studentNumber;
        cell2.innerHTML = studentInfo[index].firstName + " " +
            studentInfo[index].lastName;
        cell3.innerHTML = studentInfo[index].
        '<input type="checkbox" name="student attendance" value="absent" id="checkboxab"</input>';

    }

    function save() {
        if (document.getElementById('checkboxab').checked) {
            alert("checked");
        } else {
            alert("You didnt check it")
            studentInfo.points++
        }
    }
}

Upvotes: 1

Views: 4288

Answers (3)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

. use in php to concat string

+ use in javascript to concat string

try like this

cell3.innerHTML =studentInfo[index]+
        '<input type="checkbox" name="student attendance" value="absent" id="checkboxab"</input>';

Upvotes: 3

T-moty
T-moty

Reputation: 2797

I think that you are confusing PHP string concatenating with Javascript string concatenating:

PHP

$variable = $other_variable . '<span>hello world</span>'

Javascript

var variable = other_variable + '<span>hello world</span>'

I get it?

Upvotes: 2

phpmeh
phpmeh

Reputation: 1792

The dot is a syntax error. You should use a +.

  cell3.innerHTML = studentInfo[index].
    '<input type="checkbox" name="student attendance" value="absent" id="checkboxab"</input>';

If you don't have another way to validate your JS, put it into a jsfiddle and press JSHint.

http://jsfiddle.net/mgb8x7pd/

Upvotes: 0

Related Questions