checking if value in dropdown list

14,521

Solution 1

The value is not the label of the option but the HTML tag within the <option>.

See this working example: http://jsfiddle.net/ZZaSW/

[EDIT] Another example more like your HTML (but a <td> without a table doesn't work so I've put a <div> for the example).

Here is the HTML:

<div id="filter_analyst">
    <select>
        <option value="mike1">mike1</option>
        <option>mike2</option>
        <option>mike3</option>
    </select>
</div>

And the js:

var t = $('#filter_analyst').find('option[value=mike1]').length > 0;

Solution 2

http://jsfiddle.net/BFMD4/37/

<table>
    <tr>
        <td id="filter_analyst">
            <select id="dropdown1">
                <option value='mike1'>mike1</option>
                <option>mike2</option>
                <option>mike3</option>
            </select>
        </td>
    </tr>

//Find by value
alert($("#dropdown1 option[value='mike1']").length);
//Find by text
alert($("#dropdown1 option:contains('mike1')").length);

In your original example, the td selector was not working because it was missing the wrapping table and tr tags.

Share:
14,521
Mike
Author by

Mike

Updated on June 04, 2022

Comments

  • Mike
    Mike almost 2 years

    I have the list:

    <td id="filter_analyst">
    <select>
        <option>mike1</option>
        <option>mike2</option>
        <option>mike3</option>
    </select>
    

    and i'm trying to check if some value exist:

    var name = 'mike1';
    alert(name);
    var t = ($("#filter_analyst").find("[value='"+name+"']").length) > 1;
    alert(t);
    

    But getting false...

    Am I doing something wrong? http://jsfiddle.net/BFMD4/32/

    Thanks in advance!

  • Royi Namir
    Royi Namir over 12 years
    he wants by name not by value !
  • Admin
    Admin over 12 years
    it's correct html syntax, so he should change his html according to proper standart
  • Royi Namir
    Royi Namir over 12 years
    @Reigel , option:contains its fine ( ive added option prefix)
  • JMax
    JMax over 12 years
    That's a better shot. Here is the working jsfiddle btw: jsfiddle.net/xX3nw
  • Reigel Gallarde
    Reigel Gallarde over 12 years
    no! it's not... ;) :contains() will also return true for I am mike1... not good... isn't it?
  • JMax
    JMax over 12 years
    @RoyiNamir: That's why I posted it as a comment of your answer but you could have posted it by yourself. This is a better way to show that it works (or not).
  • JMax
    JMax over 12 years
    @Reigel: thanks for the explanation of why :contains() is a bad choice
  • Royi Namir
    Royi Namir over 12 years
    @Mike , Ive edited and tested. this is the working final solution
  • Kristoffer Lundberg
    Kristoffer Lundberg over 12 years
    You're right! With single words this work, but with a sentence you have to have ''. Try this jsfiddle.net/BFMD4/44
  • Viku
    Viku almost 9 years
    @Jmax If value will be coming as dynamic as an function argument then how can i make the above solution work ? The following does not work E.g :: function (strVal){var t = $('#filter_analyst').find('option[value=strVal]').length > 0; }