user990016
user990016

Reputation: 3378

jquery - select an option based on data

I'm trying to select a particular option based on what I get from a query;

$('#Adjunct_Title_Number > option[value=' + response.Adjunct_Title_Number + ']').attr('selected','selected');

I simply get nothing selected. I know that the value of 58 is being inserted into the statement.

Upvotes: 1

Views: 66

Answers (5)

kwarunek
kwarunek

Reputation: 12577

First of all check if $('#Adjunct_Title_Number > option[value=' + response.Adjunct_Title_Number + ']') is a defined item (not empty object).

Next you should on older jQuery to remove attribute selected from other items:

$('#Adjunct_Title_Number option').removeAttr('selected').filter(function(){
    return ($(this).value == response.Adjunct_Title_Number) })
.attr('selected','selected');

Upvotes: 0

asymptoticFault
asymptoticFault

Reputation: 4529

Try surrounding the value with quotes:

$('#Adjunct_Title_Number > option[value="' + response.Adjunct_Title_Number + '"]').attr('selected','selected');

Upvotes: 0

DevlshOne
DevlshOne

Reputation: 8457

$('#Adjunct_Title_Number option').filter(function() {
    return this.value == response.Adjunct_Title_Number;
}).prop('selected','true');

Upvotes: 0

Andy
Andy

Reputation: 30135

You don't set a select box by setting selected on the options, but by setting the value of the select box with val():

$('#Adjunct_Title_Number').val(response.Adjunct_Title_Number);

Upvotes: 1

Jason P
Jason P

Reputation: 27012

Try this instead:

$('#Adjunct_Title_Number').val(response.Adjunct_Title_Number);

Upvotes: 1

Related Questions