I dont quite this syntax error I am having when it keeps saying else { in code below:
$(document).ready(function () {
var courseinfo = <?php echo json_encode($courseInfo);?> ;
$('#coursesDrop').change(function () {
var courseId = $(this).val();
/*
You only need to do all of this if user selects a course, so check that first.
*/
if (courseId !== '') {
/*
Iterate over courses and, if the one we want exists, populate its info.
*/
for (var i = 0, l = courseinfo.length; i < l; i++) {
if (courseinfo[i].CourseId == courseId) {
$('#currentDuration').val(courseinfo[i].Duration);
$('#newDuration').val(courseinfo[i].Duration);
$('#currentCourseId').val(courseinfo[i].CourseId);
$('#newCourseId').val(courseinfo[i].CourseId);
var text = $(this).find('option:selected').text();
var split = text.split(' - ');
$('#currentCourseNo').val(split[0]);
$('#currentCourseName').val(split[1]);
/*
Without this break, the loop will continue until i = l.
We've already found our match, no need to continue.
*/
break;
}
}
} else {
$('#currentCourseNo,#currentCourseName,#currentDuration,#currentCourseId').val('');
}
});
});
Brackets seems correct but why am I getting this syntax error for the code above?
The exact error says Syntaxerror: syntax error and then in the view page source it just higlights this else{.
var courseinfo = <?php echo json_encode($courseInfo);?> ; outputs this below in page source:
var courseinfo = [{"CourseId":1,"CourseNo":"INFO101","CourseName":"Bsc Information Communication Technology","Duration":"4"},{"CourseId":2,"CourseNo":"INFO102","CourseName":"Bsc Computing","Duration":"3\/4"},{"CourseId":8,"CourseNo":"INFO103","CourseName":"Business and Finance","Duration":"3"},{"CourseId":9,"CourseNo":"INFO107","CourseName":"Mathematics","Duration":"4"}];
I can’t sure the problems are located …
Because you don;t show any html line …
But would you like to try this code:
If it won’t work, i say sorry okay ….
Good luck