Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6710967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:05:01+00:00 2026-05-26T08:05:01+00:00

:contains just isnt doing it for me here. Its too coarse. and I cant

  • 0

:contains just isnt doing it for me here. Its too coarse. and I cant seem to figure out the right way to get it.

(.js fiddle below)

$(function() {
    $('#my_button').click(function() {
        var cookieVal = "ab"; //This is what I want to find
        var select = "combo0";

        var optionThatMatchesCookie = $("#" + select + " option:contains('" + cookieVal + "')").val();
        alert(optionThatMatchesCookie); //returns the wrong entry! (returns 'cabd' not 'ab')

        if (typeof(optionThatMatchesCookie) != "undefined") {
            //now put that in the combobox
            $('#' + select).val(optionThatMatchesCookie);
        }
    });
});

<select class="inputfield" id="combo0" name="jobType">
    <option selected="selected">-select-</option>
    <option id="1">sssssssssss</option>
    <option id="18">fffffffffff</option>
    <option id="47">cabd</option>
    <option id="3">LPN/LVN</option>
    <option id="22">bbbbb</option>
    <option id="17">hhhhhhhhhh</option>
    <option id="15">aaaaaaa</option>
    <option id="16">zzzzzzzzzzzzzzz</option>
    <option id="5">ab</option>
    <option id="44">YYYYYYYYY</option>
    <option id="19">XXXXXX</option>
</select>
<input type="button" id="my_button" value="test me"/>

http://jsfiddle.net/KGabbert/mCDer/3/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:05:02+00:00Added an answer on May 26, 2026 at 8:05 am

    It returns the cabd option because that contains “ab”, and that’s exactly what :contains is used for. If you’re wanting to return the value of the option that exactly matches, you could use filter:

    var optionThatMatchesCookie = $("#" + select + " option").filter(function() {
         return $(this).val() === cookieVal;  
    }).val();
    

    Here’s a working example.

    As an aside, note that typeof is an operator, not a method, so you shouldn’t put the operand in parentheses:

    typeof optionThatMatchesCookie
    

    Also note that you could shorten your code significantly to just this:

    $(function() {
        $('#my_button').click(function() {
            var cookieVal = "ab",
                select = "combo0";
            $("#" + select).val(cookieVal); 
        });
    });
    

    If cookieVal does not match the value of one of the option elements then the value will not change. See another working example here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MVC isn't generating the client-side validation rules for my viewmodel. The HTML just contains
i have a screen contains just two components 1-textView 2-button the textView starts from
i have a menu that contains just one item. Button exit; @Override public boolean
I installed from kubuntu's package management this handy pnglite library. It contains just one
I have two tables, one for routes and one for airports. Routes contains just
It seems to be a problem with magic quotes. The original string just contains
I just built a WPF form that contains a ListBox. The ListBox has bound
i´ve just created an iOS app that contains lots of media. Now i just
I just noticed the HashTable objects have a Contains and CotainsKey method, with same
I have just a basic html form within index.php which contains two text fields:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.