1

I am using seletize 0.11.0 for bootstrap2. I want get the text from <option> with specific value.

I tried:

$("#id_sel option[value='1']").text();

I got nothing.

The html element <select> has attribute display as none.

Well, how to do that? Thanks for your consideration.

EDIT

Samples

<body>
    <select id="id_sel" multiple='multiple'>
        <option selected='selected' value='1'>first</option>
        <option value='2'>second</option>
        <option selected='selected' value='3'>third</option>
    </select>
</body>

js

$("#id_sel").selectize();
alert($("#id_sel option[value='1']").text());

NOW I KNOW

selectize remove text between option tags and make the display of select as none. It is wired.

so select at runtime would looks like:

<select id="id_sel" multiple="multiple" class="selectized" style="display: none;">
    <option selected='selected' value='1'></option>
    <option value='2'></option>
    <option selected='selected' value='3'></option>
</select>

Well I still dont know how to get text.

6
  • 3
    can you share the html sample Aug 27, 2014 at 7:31
  • which html element has attribute display none (select or option) Aug 27, 2014 at 7:36
  • I put it on jsfiddle
    – Nick Dong
    Aug 27, 2014 at 7:45
  • <select> has attribute display none. @AnkitAgrawal
    – Nick Dong
    Aug 27, 2014 at 8:02
  • Have you tried with selectize.getValue(); ?
    – urbz
    Aug 27, 2014 at 8:30

4 Answers 4

0
<script>  

    jQuery(document).ready(function()
    {
        $("#id_sel option").each(function()
        {
            if(!$(this).is(':visible'))
            {
                alert($(this).val());
            }
        });                        
    });

</script>



<select id="id_sel">
    <option>select</option>
    <option style="display: none;">1</option>
    <option>2</option>
</select>

by this code you can get value of hide option field

1
  • It seems not work for text(). I need get text between <option> and </option>.
    – Nick Dong
    Aug 27, 2014 at 7:52
0

Well, there should be some api to get that. If not, it is weired. I have tried getOption() and getItem(). It is not work. Selectize do not add text to option tags for the edition which I used. It make <select> as none, and create a group of divs to cover the position of <select>. In my edition, it looks like:

<div class="selectize-control multi">
    <div class="selectize-input items full has-options has-items">
        <div data-value="1" class="item">first</div>
        <div data-value="2" class="item">second</div>
        <input ....
        ....
    </div>
</div>

For now, I use jquery multiple selector to get text.

$(".selectize-control .selectize-input div").each(function() {
    if (1 == $(this).data("value")) {
        alert($(this).text());
    }
});

Or identified by id

$("#id_sel").siblings(".selectize-control").find(".selectize-input div").each(function() {
    if (1 == $(this).data("value")) {
        alert($(this).text());
    }
});

Hope this helpful for other people. The best answer should be use api to do this.

0

You can use

$("#id_sel option").each(function()
{
    // here you can use $(this).text()
});
2
  • I'd like to get the text of option not the value.
    – Nick Dong
    Aug 27, 2014 at 7:58
  • then you can use $(this).text() Aug 27, 2014 at 10:22
0

Here's the code that works as you mentioned:

$("#id_sel option").val("1").text();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.