,

Jquery error uncaught exception Syntax error unrecognized expression

Jquery logo

Solution for Syntax Errors

  • Change selector (e.g. instead of input[type='text'] place a class in these text fields and change selector)
  • Check if the element has an ID (e.g. instead of $('#'+this.id) use $(this))


Causes

What causes this error? "Uncaught exception: Syntax error, unrecognized expression"

  1. Your selector didn't match any elements of the DOM
  2. You are looping through objects using a selector that is not always true
  3. Selector is in the wrong format -- see "Selector Syntax" further down the page


Good Code

// Selecting all input values of a div
$('#div_id input').each(function()
    {
    if(this.id)
        {
        fields_to_save.push(this.id);
        }
    });

Bad Code

// Selecting all input values of a div
$('#div_id input').each(function()
    {
    fields_to_save.push(this.id);
    });


Selector Syntax

This happens mainly because the older versions of jQuery had different syntax.

Good Code

$("input[id=name]").val();

Bad Code

$("input[@id=name]").val();// Old style

See detailed documentation for good jQuery syntax formats.