I keep getting the "unexpected token function" error when trying to run the following code. The error appears at the if(highPriority(priority)) {
line. I appreciate any help, thanks.
$(function(){
$("input[name=task]").focus();
$("body").on("click","#add",function(){
var task = $("input[name=task]").val();
var priority = $(this).parent().children("input[name=priority]");
var doneSpan = '<span class="done">Done</span>';
if(highPriorityChecked(priority)) {
$("#todo").prepend("<p class='row high-priority'>" + task + doneSpan + "</p");
} else {$("#todo").append("<p class='row normal-priority'>" + task + doneSpan + "</p");
};
resetForm();
doneList();
});
}
function highPriorityChecked(priority){
return $("input[name=priority]").is(":checked");
};
function doneList(){
$("#todo").on("click",".done",function(){
var row = $(this).parent().detach();
$("#done").prepend(row).remove(doneSpan);
});
}
function buildRow(task, priority) {
var row = '<div class="row item ' + priority + '">' + task;
var doneSpan = '<span class="done">Done</span>';
return row + doneSpan + "</div>";
};
function resetForm() {
$("input[name=task]").val("").focus();
$("input[name=priority]").removeAttr("checked");
};
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
To solve the "Uncaught SyntaxError Unexpected token 'export'" error, set the type of your <script /> tags to module , e.g. <script type="module" src="index. js"></script> . The type="module" attribute is supported in all modern browsers and allows us to use the ES6 modules syntax.
So, to fix the unexpected token problem, go through your codes for punctuation errors. Punctuation marks like parenthesis and commas must be proper in your coding. If not, JavaScript won't recognize it and won't be able to parse it.
adeno gave the right answer in his comment
You didn't close the
document.ready
function properly, it's just closed by a single curly bracket.
He suggested you might have notice if you had formatted your code properly. But we can do better than that.
In general, when you get a syntax error in JavaScript, try running the entire script through jslint. This may help point you at the cause of the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With