13

too add a placeholder in select2 i have to add an empty option tag in the code like this

<select id="e2" name="rol_id" > <option><option> {foreach from=$data.roles item=rol} <option value={$rol.rol_id}>{$rol.rol_nombre}</option> {/foreach} </select> 

but when i do that i get this option empty that is selectable enter image description here

how can i not show that option but still the placeholder?

Thanks!

4
  • style="display:none;" ? Commented Oct 18, 2013 at 14:45
  • that still shows the empty option @3rror404 Commented Oct 18, 2013 at 14:46
  • Maybe this is an option? jsfiddle.net/nJ8Vu Commented Oct 18, 2013 at 14:56
  • i dont know if it it because of the select being inside a modal, but the disabled option is not working =( @3rror404 Commented Oct 18, 2013 at 15:02

9 Answers 9

20

You have a syntax error on 2nd line.

<option></option> 
Sign up to request clarification or add additional context in comments.

2 Comments

in the select2 documentation, it says: When placeholder is used for a non-multi-value select box, it requires that you include an empty <option></option> tag as your first option. @Max
the empty option is fine, but select2 needs to be told the placeholder text - it will then hide the empty option...
8

It's a late answer, but it can help someone.

I have modal with a select2 input.

We need to add <option></option> for the placeholder to be shown.

HTML

<select id="Name"> <option></option> <option value="John">John</option> <option value="Brian">Brian</option> <option value="Carl">Carl</option> </select> 

If I want the placeholder to appear.

Jquery

$('#Name').select2({placeholder: 'Select a Name'}); $('#Name').select2('val', ''); 

If I want to see a value on the select2 input:

$('#Name').select2('val', 'John'); 

Comments

7

The empty option is actually for placeholder, I solved it using the code below

var s1 = $('#select2id').select2({ multiple: true, placeholder: "Select values" }); s1.val([' ']).trigger("change"); 

the s1.val([' ']).trigger("change"); did the trick

1 Comment

Here's a similar solution which uses pure HTML to prevent it from being clicked, as well as several other approaches: stackoverflow.com/a/23638053/3196753
2

leave the empty option and specify the placeholder text either via the data-placeholder attribute on the tag or via a placeholder option when initializing select2.

Comments

0

You could add a line to remove the first "empty" option after loading the options for roles. You could do it as below using jquery.

$("#selectBox option[value='']").remove(); 

1 Comment

thank you, i tried that but it removes the placeholder =( @sr.
0

Just try $('select').select2({placeholder: 'Please select...'});. Combined with an empty <option></option> should do the job ;).

Comments

0

I've just faced the same problem and simple <option></option> didn't work to me.

It seems empty option is inserted but it doesn't allocate any height so it's impossible to reach it.

I solved by just adding a non-breaking-space (&nbsp;) character (simple space didn't work too):

<option>&nbsp;</option> 

Comments

0

A little bit offtop but you may search solution for this:

If you have empty options between every select2 widget option - check your jinja tags and remove <option></option> tags if you are using jinja {% for %} loop in template.

Comments

0

all you need to put as per the official documentation and it will work :

$('#mySelect2').val(null).trigger('change'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.