rlib
rlib

Reputation: 7867

jQuery multi select bug

Is it a bug? This select works:

<select id="select_advsels" name="pixel_advsels" multiple>
<option>All</option><option>Mobvista (TS-126)</option>
</select>

jQuery('#select_advsels').val(['Mobvista (TS-126)']);

http://jsfiddle.net/w2xtU/

When two words in OPTION are separated by more than one space, jQuery fails to select:

<select id="select_advsels" name="pixel_advsels" multiple>
<option>All</option><option>Mobvista  (TS-126)</option>
</select>

jQuery('#select_advsels').val(['Mobvista  (TS-126)']);

http://jsfiddle.net/3FrR7/2/

Upvotes: 1

Views: 432

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

While html is rendered, it replaces the continues spaces with a single one :

jQuery('#select_advsels option').each(function(){
    console.log(this.value.replace(' ', '-'));
})

Demo: Fiddle

Refer this

HTML treats whitespace characters (spaces, tabs, and newlines) differently from ordinary characters. In general, a single whitespace character--including newlines--or a sequence of whitespace characters are treated as a single space and leading/trailing whitespace is eliminated. This is known as 'collapsing whitespace'. Therefore the following two paragraphs are treated as if they were identical

Upvotes: 4

Anthony Grist
Anthony Grist

Reputation: 38345

As has already been explained, the browser collapses multiple continuous whitespace characters down to a single space. If you absolutely need to preserve the double spaces then use explicit value attributes when declaring your <option> tags:

<option value="Mobvista  (TS-126)">Mobvista  (TS-126)</option>

The value displayed to the user will only have a single space, but the value sent when the form is submitted or matched when using JavaScript will retain the multiple spaces.

Updated jsFiddle

Upvotes: 1

Related Questions