Triggering an event upon selecting a dropdown option

I'm currently working on a form that includes a dropdown menu. My goal is to display specific keywords for each trip when selected from the menu (preferably below the input field, as shown in the code snippet below).

  1. .show has been set to display:none
  2. The keywords will be wrapped within paragraph tags

HTML

<label for="tour-select">Choose a tour
    <select name="tour" id="tour-select" required>
        <option value="landmarks"></option>
           <p class="landmarks show"></p>
        <option value="hidden-gems"></option>
           <p class="hidden-gems show"></p>
        <option value="diana">The Diana (5h)</option>
           <p class="diana show"></p>
    </select>
</label>

I've attempted some JavaScript code but haven't had any success yet. I'm unsure if I'm targeting the option value incorrectly or if there's a problem with my code structure.

let tourInput = document.querySelector("select[name=tour]");
    tourInput.addEventListener("click", e => {
        if (e.target.value == "landmarks") {
            tourInput.querySelector(".landmarks").classList.remove("show");
        }
    })

Answer №1

It appears that the issue lies in the absence of single quotes around the selector name. Please update "select[name=tour]" to "select[name='tour']".

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

Total aggregate for a variety of Sliders (Bootstrap 4 & jQuery)

I am currently working on a web page that involves implementing 3 sliders. The total of all three sliders should always be limited to 100%. This project utilizes the Bootstrap 4 framework and jQuery 3.6.2 Here are my current challenges: The combined valu ...

At times, the loading image fails to appear on Ajax

Take a look at my code below: function apply_image_effect(){ $.ajax({ url: "image/image.php", global: false, type: "POST", data: ({my_color:encodeURIComponent($('#my_color').val()),my_size:$('#my_size&apos ...

One beneficial aspect of utilizing JavaScript closures for events is when defining a click event

There are two common ways to write code in JavaScript that accomplish the same task: Option A. SomeObject.prototype.hideElement = function(e, element){ e.stopPropagation(); hide(element); } Option B. SomeObject.prototype.hideElement = function( ...

Unable to locate NPM-installed modules in the local directory

I've been working on a project and I encountered an issue with locally installed modules using NPM in NodeJS on Windows 10. Despite setting up NODE_PATH, I am still getting errors when trying to require these modules. Here is the structure of my proj ...

Extracting numbers using regular expressions can be tricky especially when dealing with mixed

Currently, I am attempting to create a javascript regex that can extract decimal numbers from a string containing a mix of characters. Here are some examples of the mixed strings: mixed string123,456,00indeed mixed string123,456.00indeed mixed string123,4 ...

What is the method of adding a child to the outerHTML of the parent element instead of within it?

Is there a way to use the outerHTML method on a parent element to append a child element so that the icons appear outside of the targeted element rather than inside it? The current code snippet shows the icons inside the box, but I want them to be placed o ...

Testing an Angular component with @Input through unit testing

I am dealing with an angular component that utilizes an @input attribute and processes it in the ngOnInit. When writing unit tests for @inputs, I usually set the value using component.inputproperty=value. However, in this case, I cannot do so due to the ...

Adjust the div class to align with its content

I am looking to modify the code in order to copy the text of a div to its own class. Currently, the code provided copies text from all sibling div elements, but I specifically want each individual div's text to be its own class. For example, with the ...

What is the best way to extract data from a table and transmit it to the server using AngularJS?

I'm attempting to extract all the content from a table in HTML. Is it possible to retrieve all rows from one side and send them in a post request to the server? I've been struggling to figure out how to do this. Do I need to bind each row using n ...

Guide to getting Material-UI Dialog up and running smoothly in your React application

I'm currently working on a project using material-UI, and I'm having trouble with the Dialog component not working properly. Despite trying various solutions, I haven't been able to get it to function correctly in my React application with m ...

The steps to view the JavaScript file of a website using a web browser's inspector tool

As I was exploring the web browser inspector tool, I attempted to view the JavaScript source code of a website (). However, all I could find was HTML code (view-source:). I am eager to access the JS file from my browser directly. How can I achieve this? I ...

Changing the appearance of a website by switching CSS stylesheets according to the size of the browser

Trying to build two versions of my website: one for mobile devices or small browsing windows, and another for large browser windows. Looking to link HTML to different style sheets based on browser size. Current attempt in code only addresses total screen ...

Send information to a function until the array reaches its maximum length

I am facing a challenge where I have a function that accepts multiple arrays as arguments, but the data available to me is already within an array called mainArr. This mainArr consists of several array items that need to be passed as arguments to the funct ...

Are iFrames being utilized for third-party applications?

I am looking to create a web application that can be extended by other developers with their own applications. Would using iFrames, similar to how Facebook does it, be the best approach? Is this considered a good practice in web development? Are there a ...

Ways to decrease the size of this item while maintaining its child components?

Here is an object that I am working with: { "name": "A", "children": [ { "name": "B", "open": false, "registry": true, "children": [ { ...

Generating dynamic slots in VueJS allows for the creation of

Creating slots dynamically from an array is my current task. After some tinkering, I've managed to make it work using the following code snippet: <template v-for="(department,id) in departments" v-slot:[id]="record"> < ...

Utilize Protractor to extract the text within a specified span element

Is it possible to check if the text of the span within the button is "Vigente" using expect and Jasmine? If so, how can I achieve this? <button _ngcontent-hke-28="" class="btn btn-success disabled" tabindex="-1"> <span _ngcontent-hke-28="" class= ...

Is relying on getState in Redux considered clunky or ineffective?

Imagine a scenario where the global store contains numerous entities. Oranges Markets Sodas If you want to create a function called getOrangeSodaPrice, there are multiple ways to achieve this: Using parameters function getOrangeSodaPrice(oranges, s ...

"Enhancing User Experience with Multiple Conditional Checkboxes in jQuery

Having difficulty making a checkbox change some HTML content using JQuery. There is a standard html checkbox with the following attributes <input type="checkbox" name="AQN1" class="checkbox Q1" value="AQN10" id="3mcq"> <input type="checkbox" nam ...

The prop HandleClick is not being identified

Whenever I click on the sidebar, my menu should appear, but instead I'm encountering an error message saying "react-dom.development.js:86 Warning: React does not recognize the handleClick prop on a DOM." import { useState } from "react"; imp ...