0

Basically I need help with select2. Right now I have a search box with this JQuery code in the head. When I preview it there's no place holder either.

<script type="text/javascript">

$(document).ready(function() {
  $('select').select2({
  placeholder: "Select a Townhall Level",
    allowClear: true
  });
});
</script>
</head>

Here's an image: enter image description here

So I wonder how do I hide the search too? I have no experience at all with JQuery so help would be appreciated. Thank you for taking your time to read!

Here's my html: http://pastebin.com/P8in7ASX

1 Answers1

1

You can hide the search by setting minimumResultsForSearch like this:

$(document).ready(function() {
  $('select').select2({
    placeholder: "Select a Townhall Level",
    minimumResultsForSearch: Infinity,
    allowClear: true
  });
});

And you can show the placeholder if you have an empty option as the first option in your select:

<select>
    <option></option>
    ... other options ...
</select>

If you want to target specific selects rather than all of them, with the ability to apply different properties to each, change to this:

$(document).ready(function() {
  $('#targetElement1').select2({
    ... the properties you want applied to targetElement1 ...
  });

  $('#targetElement2').select2({
    ... the properties you want applied to targetElement2 ...
  });  
});

And give your selects IDs:

<select id="targetElement1">
    <option></option>
    ... other options ...
</select>

<select id="targetElement2">
    <option></option>
    ... other options ...
</select>
Mark Leiber
  • 3,118
  • 2
  • 13
  • 22