Skip to content

Instantly share code, notes, and snippets.

@daffydowden
Created September 30, 2010 09:10
Show Gist options
  • Save daffydowden/604267 to your computer and use it in GitHub Desktop.
Save daffydowden/604267 to your computer and use it in GitHub Desktop.
selecting an option from a dropdown with jQuery
//on drop down selection, select the element in the hidden input
$('.qa-filter-select ul a').live('click', function(){
$('#category option[value=selected]').val('')
$('.qa-filter-select option').eq($(this).index('.qa-filter-select ul a')).val('selected','selected');
$('.qa-filter-select .selected').html($(this).html());
});
@daffydowden
Copy link
Author

Intended to set the selected attribute to 'selected', but instead it just sets the value attribute to 'selected'. Also tried .attr('selected', 'selected') too

@daffydowden
Copy link
Author

Solution:

$('.qa-filter-select ul a').live('click', function(){
$('#category option')['selected'] = false;
$('.qa-filter-select option')[$(this).index('.qa-filter-select ul a')]['selected'] = true;
$('.qa-filter-select .selected').html($(this).html());
});

Seems like .attr('selected','selected') should do this, but it doesn't. This doesn't visibly manipulate the dom though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment