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

  • SEARCH
  • Home
  • 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 7636653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:44:13+00:00 2026-05-31T07:44:13+00:00

Using JQuery, I’m trying to set an option as selected in a ‘select’ element

  • 0

Using JQuery, I’m trying to set an option as selected in a ‘select’ element based on the query string.

This question is similar to this, however I still need to know how to check if the element exists before performing the selection, otherwise the page will refresh itself continously (see the exit condition below).

Fetching the query string is done using the function getParameterByName, and it’s working fine.

The current implementation below:

function setSelectedItem(selectName, itemToSelect) {
    ///<summary>Selects an HTML option element inside a HTML select element based on the value from the query string.</summary>
    ///<param name="selectName" type="string">The partial name of the HTML select in which 'itemToSelect' must be selected</param>
    ///<param name="itemToSelect" type="string">The name of the query string parameter which value is the of the 'option' element to select.</param>

    //If an items has already been selected, return
    if ($('select[name*=' + selectName + ']')[0].selectedIndex != 0) return;


    //Fetches the value from the query string; if empty, returns
    var valueToSelect = getParameterByName(itemToSelect);
    if (valueToSelect == "") return;

    //Fecthes the HTML select object
    var selectObject = $('select[name*=' + selectName + ']');

    //HERE how to check if 'valueToSelect' does not exist and return?

    selectObject.val(valueToSelect).attr('selected', 'selected').change();
}

Update: The solution which worked was:

    //Checks if the option exists, and returns otherwise
    if (!selectObject.find('option[value=' + valueToSelect + ']').length)
        return;
  • 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-31T07:44:14+00:00Added an answer on May 31, 2026 at 7:44 am

    try to check selectObject.find('option[value="'+valueToSelect +'"]').length > 0

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

Sidebar

Related Questions

Using jQuery, I'm trying to change the selected option in a SELECT element to
using jQuery I have the following code: var selectedIdsArray = $(#selectId option:selected).map(function(){return this.value;}); var
Using jQuery , how can I dynamically set the size attribute of a select
Using jQuery, how do you check if there is an option selected in a
Using jQuery 1.6.2 HTML that I'm operating on <select id=langId> <option value=0>Argentina - Spanish</option>
Using jQuery UI Resizable I'm trying to prevent resizing based on various rules. The
Using jQuery, I'd like to display a different set of text based on a
Using JQuery, how do I select all elements with class x within an element
Using jquery how do I focus the first element (edit field, text area, dropdown
Using jQuery, what's the best way to find the next form element on the

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.