VladislavLysov VladislavLysov - 10 days ago 5
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

Put quotes around your option :

$('select[name="location"] option[value="' + 'mercedes mercedes' + '"]')
Comments