VladislavLysov VladislavLysov - 1 year ago 120
Javascript Question

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?

Answer Source

Put quotes around your option :

$('select[name="location"] option[value="' + 'mercedes mercedes' + '"]')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download