Syntax error, unrecognized expression: option[value=property name]
Asked Answered
O

3

6

I have a datalist which looks like this

<datalist id="properties">
       <option value="property name"></option>
       <option value="property"></option>
</datalist>

Now I'm using this code to find where values entered by the user is in the list:

var user_property = $('#user_property').val().toLowerCase(); // taken from input type with id user_property
var pro = $('#properties').find("option[value="+user_property.replace(' ','-')+"]");
if(pro != null && pro.length > 0)
{
    // run some code
}
else
{
    // show error popup
}

I am getting error in var pro = $('#properties').find("option[value="+user_property.replace(' ','-')+"]");

Error code says Syntax error, unrecognized expression: option[value=property name]

How to get rid of this error?

Orifice answered 28/1, 2015 at 17:28 Comment(0)
C
20

try adding quotes, as:

var pro = $('#properties').find("option[value='"+user_property.replace(' ','-')+"']");

or better break it down to:

var replaced = user_property.replace(' ','-');
var pro = $('#properties').find("option[value='"+replaced+"']");

if you want to check for text like "property name" then you could directly do:

var pro = $('#properties').find("option[value='"+user_property+"']");
Collocutor answered 28/1, 2015 at 17:31 Comment(4)
that would work, but option list has "property name" so it would return falseOrifice
@Orifice if you want to check for "property name" then there's no need to do a replace.. as in my added answer..!Collocutor
this is weird, your second solution is working on firefox but not in chrome :\Orifice
it's funny, it stopped working on firefox and chrome all of a sudden, then i realized source code of the page wasn't updated, then i ditched sublime text for aptana :D any way thank you for you answer :)Orifice
A
3

Try adding quotes around the value and it will work.

$('#properties').find("option[value='property name']")
Apothecium answered 28/1, 2015 at 17:31 Comment(0)
E
1

You need to add single quote for your value like

var pro = $('#properties').find("option[value='"+user_property.replace(' ','-')+"']");
Enkindle answered 28/1, 2015 at 17:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.