Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery exception when select option with whitespaces

I have a problem with select option in html select element via JQuery. Next code demonstrate my problem:

$('select[name="location"] option[value=' + 'mercedes mercedes' + ']').attr('selected', true);

And i have simple html code for select with option value - "mercedes mercedes". When I run my script - i got exception:

"Error: Syntax error, unrecognized expression: [value=mercedes mercedes] throw new Error( "Syntax error, unrecognized expression: " + msg );"

When I delete whitespace - all is ok. Maybe anyone know - how to fix it?

like image 202
VladislavLysov Avatar asked Dec 07 '22 11:12

VladislavLysov


1 Answers

Put quotes around your option :

$('select[name="location"] option[value="' + 'mercedes mercedes' + '"]')
like image 61
Denys Séguret Avatar answered Dec 11 '22 10:12

Denys Séguret