Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  189] [ 2]  / answers: 1 / hits: 24847  / 10 Years ago, wed, january 28, 2015, 12:00:00

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?


More From » jquery

 Answers
8

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+']);

[#68058] Monday, January 26, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;