Search code examples
jqueryruby-on-railsjquery-select2ui-select2select2-rails

how to add conditional to blank select2 option? :include_blank => true?


When the select option is the blank placeholder, how can I find this, so I can execute the appropriate jquery / javascript?

here's my markup (haml) :

= form_tag(compare_path, method: 'get', remote: true, id: 'compare_form') do
          = select_tag "votus_friend_id", options_from_collection_for_select(@votus_friends, :id, :full_name, @comparison.votus_friend.id), :include_blank => true, :class => "compare-search", :style => "width:100%; margin-left: 3px;"

here's my JS. I know it's way off here. I can't seem to find the exact value that's coming back from select2 for the blank place holder in order to run the conditional:

$('.compare-search').on('change', function() {

  console.log('thisss', $('<select/>')  );

  if ($('.compare-search').val() === '') {
    $('.vScoreExplain').show();
    $('.stats').hide();
  }
  else {
    $('.vScoreExplain').hide();
    $('.stats').show();
  }
});

Solution

  • You can try this:

    $('.compare-search').on('change', function() {
    
      if ($(this).val().length) {
        $('.vScoreExplain').hide();
        $('.stats').show();
      }
      else {
        $('.vScoreExplain').show();
        $('.stats').hide();
      }
    });