Andy
Andy

Reputation: 137

Select list option isnt being set with Jquery

Select list:

<select id="Outcome#1" class="result" name="Outcome#@1">
                            <option value="x">Select</option>
                            <option value="4">Good</option>
                            <option value="3">Adequate</option>
                            <option value="0">Not Applicable</option>
                        </select>

Jquery

$("#Outcome#1").val("4");

The above select list is being dynamically created from a DB call, and the javascript is also being created on the fly and the val being populated If one exists in the DB.

However, in the above example, the dropdown isn't being set to have Option 4(Good) set to Selected.

I'm under the impression that the above bit of JQuery should do this quite happily.

Also tried: (and failed)

$("#Outcome1[value='4']").attr("selected", "selected");

Any ideas?

Upvotes: 0

Views: 228

Answers (3)

adeneo
adeneo

Reputation: 318172

You need to escape the hashsign

$("#Outcome\\#1").val("4");

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337560

Your selector is actually looking for an element with an id attribute of both Outcome and 1 - which is impossible. You need to escape that # in the selector itself, try this:

$("#Outcome\\#1").val("4");

Upvotes: 2

Anthony Grist
Anthony Grist

Reputation: 38345

The # in the id attribute is a meta-character, so will need to be escaped as part of your jQuery selector:

$('#Outcome\\#1').val("4");

This is explained at the top of the Selectors API page.

Upvotes: 4

Related Questions