Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected token function

Tags:

jquery

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");
 };
like image 949
user1644644 Avatar asked Sep 03 '12 21:09

user1644644


People also ask

What does uncaught SyntaxError unexpected token mean?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

How do I fix uncaught SyntaxError unexpected token export?

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.

How do I fix unexpected token parsing error?

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.


1 Answers

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.

like image 190
Day Avatar answered Oct 21 '22 12:10

Day