Monday 26 September 2016

jquery : How to select an option by its text?



down voteaccepted
This could help:
$('#test').find('option[text="B"]').val();
This would give you the option with text B and not the ones which has text that contains B. Hope this helps
EDIT:
For recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();

jQuery( document ).ready(function() {
    var company_type = $("#user_company_type").val();// Option text val
    var user_type_id = $('#user_user_type_id option').filter(function () {  
               return $(this).html() == company_type }).val()
    document.getElementById('user_user_type_id').value = user_type_id;
});



                           
                           
                           
                           
                           

               RAILS SELECT TAG WITH FORM OR WITHOUT FORM

                           
= select_tag 'user_types_ids[]',options_for_select( 
       UserType.all.map {|data| [data.name, data.id]},@selected_user_types),
      {:multiple => :multiple,:id=>"user_types_ids"}



= f.select :knowledge_base_category_id, options_for_select(KnowledgeBaseCategory.all.map {|data| [data.name, data.id]},@article.knowledge_base_category_id)

                           

No comments:

Post a Comment