Send a variable to a specific page and locate it within a dropdown menu

I am currently working on an HTML page that displays employee leave details. The page includes a pending leave option with the ability to edit. Users should be able to edit their leave while it is still marked as PENDING. Once the edit button is clicked, the corresponding row details will be sent to the update page. Additionally, I want the leave type to be displayed in a combo box and for the selected value to be highlighted.

For instance, if I click the edit button under Casual Leave category, the desired output would be:

<select id="select_type">
    <option value="Earned Leave">Earned Leave</option>
    <option value="Casual Leave" selected>Casual Leave</option>
</select>

P.S: It is essential to pass the variable through JavaScript. To achieve this, my JavaScript function to pass the variable looks like this:

function GetUrlValue(VarSearch){
var SearchString = window.location.search.substring(1);
var VariableArray = SearchString.split('&');
for(var i = 0; i < VariableArray.length; i++){
    var KeyValuePair = VariableArray[i].split('=');
    if(KeyValuePair[0] == VarSearch){
        return KeyValuePair[1];
    }
}
}
var x = decodeURIComponent(GetUrlValue('ReqType'));
var y = decodeURIComponent(GetUrlValue('FromDate'));
var z = decodeURIComponent(GetUrlValue('ToDate'));
var z1 = decodeURIComponent(GetUrlValue('NoDays'));

To set the value of the combo box to the passed variable and highlight it as selected, please follow these steps. I hope this clarifies any confusion.

Answer №1

If you have the choice of option value, you can implement it in the following ways:

var selectedValue = yourvalue;

$('#select_type option[value='+selectedValue +']').attr('selected','selected');

Alternatively, you can also do it like this:

$("#select_type").val(selectedValue);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When utilizing Backbone.js on(), what is the final reference for this object?

I've been diving into Backbone and I'm a bit puzzled about where the 'on' function is coming from. Initially, I thought it was part of jQuery, but I must be missing something in the API. Could someone shed some light on what the 'o ...

Modify CSS class if user is using Internet Explorer version 10 or above

I am attempting to modify the CSS of 2 ASP controls using jQuery specifically for users accessing the site with Internet Explorer 10 or 11. This adjustment is necessary because IE10 onwards, conditional comments are no longer supported. My approach to achi ...

What is the best way to set the state to false upon unmounting a component in react?

Currently, I am controlling the state of a dialog using context. Initially, the isOpen state is set to false. When the add button is clicked, the state isOpen becomes true and clicking the cancel button will revert it back to false. If the user does not c ...

What is the best way to modify the attributes of an object within the state?

Although I've been working with the React framework for a few weeks now, I still find myself surprised by new challenges on a regular basis. Currently, I am focused on developing the login/game lobby/server lobby for a group project game. The concept ...

Incorporating an iFrame into a Bootstrap modal using jQuery when it is displayed

I am looking to include a YouTube embed in a modal only when the modal is visible on the screen using jQuery. Since I have multiple modals on the screen that affect page load time, I want to specifically load the video when that particular modal is visib ...

Sharing stickers with Discord.js version 13

I have encountered an issue while trying to forward messages sent to my bot via DM. Everything is functioning smoothly, except for sending stickers. Whenever I attempt to send a message with a sticker, an Error DiscordAPIError: Cannot use this sticker is ...

"Troublesome issues with the functionality of the jquery lazyload plugin

I've come across an issue with the jquery lazyload plugin in my project. It seems that the plugin doesn't kick in when the page initially loads, but rather only starts working when I click on the Home tab of my website. Below are snippets from my ...

Struggling with Nuxt 3 Firebase and VueFire: unable to get useCurrentUser to work

Currently, I am attempting to implement VueFire's useCurrentUser() function within a Nuxt 3 application that does not utilize Server Side Rendering. I have properly configured vuefire in my nuxt.config.ts file as outlined in the documentation. export ...

Implement a feature in NextJS where an MP3 file is played upon clicking

Just getting started with JS frameworks and having some trouble with the function syntax. Here's what I have so far: when the button is clicked, it should play a quick audio file specified below the button in the Audio tags. Any suggestions on how to ...

Switching Icon in Vuetify Navigation Drawer Mini Variant upon Click Event

UPDATE Here's the solution I discovered: <v-icon> {{ mini ? 'mdi-chevron-right' : 'mdi-chevron-left' }} </v-icon> Is it feasible to modify the icon when toggling between navigation drawer variants? The default varia ...

What is the process for creating a jQuery object in TypeScript for the `window` and `document` objects?

Is there a way to generate a jQuery object in TypeScript for the window and document? https://i.stack.imgur.com/fuItr.png ...

Encountered a problem during the installation of tensorflowjs for node | Received an error: Command failed: node-pre-gyp install

While attempting to install @tensorflow/tfjs-node, I encountered the following error: Command failed: node-pre-gyp install --fallback-to-build. Is there a solution to resolve this issue? Below is the complete log: npm ERR! code 1 npm ERR! path E:\ ...

GraphQL/Relay Schema "Field cannot be queried..."

I'm encountering an issue when trying to query specific fields in graphql/relay. My goal is to retrieve the "courseName" field for Courses from the schema provided below. For instance, if I query the User with: { user(id:1){firstname,lastname}} T ...

Saving HTML code entered in a textarea field does not work with the database

Recently, I encountered an issue on my website related to storing news posts in HTML format. My initial approach was to write the posts in HTML for better presentation and then transfer this code into a Textarea element. The plan was to save this input in ...

Generate visual at reduced quality (three.js)

Is there a way to reduce the resolution of my rendered canvas similar to how it can be done with the Blender camera resolution? I came across a suggestion to use renderer.setDevicePixelRatio(window.devicePixelRatio) However, when I tried this, the objec ...

Building a favorite feature in Django using HTML

Currently, I am working on implementing an Add to Favorite feature. So far, I have succeeded in displaying a button with an icon based on the value of the is_favorite field, but I am facing difficulties updating my database. I would like to know: How can ...

Creating a JSON parameterized Spring REST webservice that can be easily consumed through Jquery AJAX

I am currently in the process of learning the Spring Framework in order to develop RESTful web services for my upcoming projects. So far, I have successfully utilized the GET method and consumed data through a simple Ajax request. Additionally, I have expe ...

Retrieving Text from CSS Property with Selenium in Python

Having trouble identifying an input tag html element with Selenium Python? Not because of the wait. I need to extract text from a field on a web page with a form named Form1. Here is the html element when inspected in Chrome: Input Element: <input name ...

Verifying the content of the JSON data

If I receive JSON data that looks like this: {"d":1} Is it possible to determine whether the value after "d": is a 1 or a 0? I attempted the following method, but it always goes to the else block, even though I know the JSON data contains a 1. success: ...

Utilize Google API V3 to create a dropdown selection list of locations, extracting latitude and longitude values from a JSON text file

I am working on creating a map using Google Maps API v3. I have implemented a dropdown select list for different places and I am pulling the latitude and longitude values from it. Initially, I manually added the options like this: <option value= ...