A guide on integrating a submission button that combines values from multiple dropdown forms and redirects to a specific URL

Is there a way to make the submit button on this form act based on the combined values of two different dropdown menus?

For instance, if "west" is selected from the first dropdown and "winter" is selected from the second dropdown, I want it to navigate to one specific URL. Similarly, if "west" and "summer" are selected, it should go to a different URL.

I've been struggling for days trying to figure this out. I have a feeling that there must be a solution to achieve this. Any help would be greatly appreciated!

This is the code I am currently using. When the button is clicked, it navigates to the URL selected in the dropdown. However, I want the values from both dropdowns to be considered together to create a unique URL upon submission. In total, there should be 16 different URLs based on the combinations of options chosen.


$(document).ready(function () {
    $(".go-btn").click(function () {
        location = $("#selection option:selected").val();
    });
});

<div class="selCont">
    <h2>Pick an Option</h2>
    
    <select id="selection" name="selection">
        <option value="1">West</option>
        <option value="2">East</option>
        <option value="3">North</option>
        <option value="4">South</option>
    </select>

    <select id="selection" name="selection">
        <option value="1">Winter</option>
        <option value="2">Spring</option>
        <option value="3">Summer</option>
        <option value="4">Fall</option>
    </select>
    
    <button class="go-btn" type="submit">Go</button>
</div>

Answer №1

Give this code a try:

jQuery( document ).ready(function( $ ) {
    $( ".go-btn" ).click(function() {
        // Extract text from select boxes
        var firstSelection = $( "#selection1 option:selected" ).text();
        var secondSelection = $( "#selection2 option:selected" ).text();

        // Specify URL, modify as needed
        var url = "http://www.example.com/" + firstSelection + "/" + secondSelection;

        // Redirect to the new URL
        window.location.href = url;
    });
});

Additionally, update the IDs for your select boxes:

<select id="selection1">
    <option value="1">Fingers</option>
    <option value="2">Toes</option>
    <option value="3">Torso</option>
</select>
<select id="selection2">
    <option value="1">Pimples</option>
    <option value="2">Flaky Skin</option>
    <option value="3">Blushing</option>
    <option value="4">Delicateness</option>
</select>

Regards,

Andrew

Answer №2

To enhance the functionality, assign a unique id to both selector elements. Below is a snippet that demonstrates this:

(function () {
  
  $(".go-btn").on(
    'click', 
     function (e) {
       // Determine the URL based on selected values
       // The next line displays the text of selected values only
       $('#selected').html([$('#select1 option:selected').text(), 
                            $('#select2 option:selected').text()].join('/'));
  });
  
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div class="selCont">
  
  <h2>Choose an option</h2>

  <select id="select1">
    <option value="1" selected>Sun</option>
    <option value="2">Moon</option>
    <option value="3">Mars</option>
    <option value="4">Venus</option>
  </select>

  <select id="select2">
    <option value="1" selected>Summer</option>
    <option value="2">Autumn</option>
    <option value="3">Winter</option>
    <option value="4">Spring</option>
  </select>

  <button class="go-btn" type="submit">Go</button>
  
</div>
<div id="selected"></div>

Answer №3

In order to proceed, I have outlined the following steps:

If a user chooses:

I am: "Someone who works with my hands" and I need: "UV Protection"

Upon clicking Go, they will be directed to:

or

If a user selects: I am: "Someone who works with my feet" and I need: "Acne"

After clicking Go, they will be redirected to:

jQuery( document ).ready(function( $ ) {
    $( ".go-btn" ).click(function() {
        // Grab text from select boxes
        var firstSelection = $( "#selection1 option:selected" ).text();
        var secondSelection = $( "#selection2 option:selected" ).text();

        // Set URL, change as necessary
        var url = "http://www.example.com/" + firstSelection + "/" + secondSelection;

        // Redirect
        window.location.href = url;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div class="selCont">
  
  <h2>Me Time</h2>

I Am:
  <select id="select1">
  <option value=" " selected="selected">- Select -</option>
    <option value="1">Someone who works with my hands</option>
    <option value="2">Someone who works wit my feet</option>
    <option value="3">Someone who works with my body</option>
  </select>

and I need:
  <select id="select2">
    <option value=" " selected="selected">- Select -</option>
    <option value="1">UV Protection</option>
    <option value="2">Acne</option>
    <option value="3">Dry Skin</option>
    <option value="4">Eczema</option>
    <option value="5">Itchy Relief</option>
    <option value="6">Redness</option>
    <option value="7">Sensitive Skin</option>
  </select>

  <button class="go-btn" type="submit">Go</button>
  
</div>
<div id="selected"></div>

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

Pausing a running function in React

Exploring Visual Sorting Algorithms In the process of creating a visual sorting algorithms tool for educational purposes, I have developed a function called sortArray() that handles the animation of the sorting process on arrays. The functionality is evid ...

Auto language-switch on page load

Wondering if using jQuery is the best approach to create a single French page on my predominantly English website. I want it to work across multiple browsers on pageload, currently using HTML replace. jQuery(function($) { $("body").children().each(funct ...

Instructions for updating the Modal Value using ajax

This is the script I use to retrieve the data <script> $(document).ready(function() { $('.tmpsurat').click(function() { var id=$(this).data('id'); var url ='{{URL('/cekSuratKelengkapan')}}/'+id; ...

Struggling with handling numbers and special symbols in the Letter Changes problem on Coderbyte

Hello I have been struggling to find a solution for my current issue. The problem lies within an exercise that requires changing only alphabetical characters, but test cases also include numbers and special characters which are being altered unintentionall ...

Can an input element be used to showcase a chosen image on the screen?

I would like to display the selected image from an input element. Can this be done with a local file, accessing the image on the client side, or do I need to upload it to a server? Here is my React code attempt: I can retrieve the correct file name from t ...

Encountering an issue where I am unable to access properties of undefined (specifically 'up') within Material UI styling

I encountered an error message Uncaught TypeError: Cannot read properties of undefined (reading 'up') while executing the code snippet below: I am having trouble with compiling my react js Code Snippet from Index.js import React from 'react ...

The accumulation of classes occurs during the cloning process of a template used for a data array

Encountered an issue while duplicating a template div to generate elements for a dataset. The problem arises from classes stacking up when creating elements for each data entry. Sample JavaScript code: $(document).ready(function(){ var data = [ { ...

Troubleshooting issue: Looping through array in Node.js with Express, JSON, and Handlebars not functioning correctly with

I'm struggling to grasp the concept of looping through an object in Handlebars and transferring information from one location to another. Here is a sample json file that I am attempting to read from. The file, named "testdata.json", contains: { &qu ...

Is there a way to confine a jquery script within the dimensions of the container div?

I have recently created a gallery with navigation buttons in jQuery. As a newcomer to jQuery, I wrote a small script that adjusts the margin of a div container by a fixed amount. The script is functioning properly, but it extends beyond the top and bottom ...

Creating a custom class implementation in JavaScript and initializing it with a constructor function

Perhaps there is a similar question out there, but I haven't come across it yet and I'm still facing an issue. Here's what I've tried: function createClass(obj) { const constructor = obj.constructor; constructor.prototype = obj; ...

React- Struggling to modify state from child component using function declared within a parent component

This is my main component: import React, {useState} from 'react'; import SearchBar from '../components/SearchBar'; import WeatherDisplay from '../components/WeatherDisplay'; import LocationInfo from '../components/Locat ...

Using Javascript and jQuery to create an infinite animated background change with fade effect

I am struggling to figure out how to automatically change the background of a div every 3 seconds, looping through a series of images. I posted about this issue before but didn't receive much help. function animate() { change1(); change2() ...

Unable to alter Mui input label color upon focus using theme.ts

In my next.js app, I'm facing an issue with changing the color of the label in a Material UI input field using Mui. Despite updating the theme.ts file to change the border bottom color and label color, only the border bottom color is being applied. T ...

Can you explain the significance of "javascript:void(0)"?

<a href="javascript:void(0)" id="loginlink">login</a> The usage of the href attribute with a value of "javascript:void(0)" is quite common, however, its exact meaning still eludes me. ...

Using conditional statements to render content based on a certain condition within a

One of my requirements is to dynamically render a React component in the following manner: parent.ts ... <Parent> <Child/> <Parent> ... child.ts ... return (someBoolean && <Component/>) ... While ...

Looking to import a 3D gltf model using the input tag in an HTML file for use in three.js (JavaScript), but encountering an issue with the process

I am trying to upload my 3D model from an HTML input tag, but I keep encountering this error message: t.lastIndexOf is not a function Here's the HTML code I am using: <input type="file" id="MODEL" /> <button onclick=&qu ...

What are the best practices for managing mouse events in AlpineJS when working with my menu?

I'm currently tackling the challenge of developing a mega dropdown menu feature using alpine.js and Tailwind CSS. Unfortunately, I've hit a roadblock as I can't seem to get the mouse events functioning correctly. In the snippet below, you&ap ...

Tips for setting a jQuery variable equal to the value of a JSON object

When I try to assign courseid and batchid as defaults using defaultValue => defaultValue: courseid and defaultValue: batchid, the values are not being saved correctly in my database. $(document).ready(function() { var courseid = null; var bat ...

What methods could I use to prevent the WYSIWYG buttons from automatically linking?

I've been working on creating an editor but I'm facing a small issue. Every time I click on a button (such as bold or italic), it follows a link instead of performing the desired action. Here's a snippet of what I've tried so far: fu ...

Troubles with Promise.all and json() in JavaScript causing errors being logged as "invalid function"

I'm experiencing some difficulties with the "Promise.all" method. Essentially, I have an array of URLs (here is a simple one if you want to test it: const urlArray = [ "https://coverartarchive.org/release/985adeec-a1fd-4e79-899d-10c54b6af299&qu ...