Using the jQuery Selector with multiple IDs will yield different results compared to using a single ID selector

Initially, I set up some dropdown menus to determine which one has been selected and then load the elements with no issues.

However, when I added a new set of dropdowns, my existing function started applying to this one as well because it was too broad.

$(document).on('click', '.dropdown-menu li a', function () {...}

Now, I have these dropdowns on four different tabs in a modal, and I only want the last tab to have different functionality than the other three. Therefore, I need to create a function that works for the original three tabs and another for the fourth type of tab.

IDs for the first three tabs:

#racksTab
#condenserTab
#glycolTab

Fourth tab (different): #alertTab

My question is, why do I get different results when my selector uses multiple IDs compared to just one?

$(document).on('click', '#racksTab,#condenserTab,#glycolTab .dropdown-menu li a', function () {...}

This function gives me https://i.sstatic.net/GRoXV.png

But if I make the selector use only one id, it gets the child node I want.

$(document).on('click', '#racksTab .dropdown-menu li a', function () {...}

https://i.sstatic.net/33Uu8.png

Answer №1

When using the following selector:

#racksTab,#condenserTab,#glycolTab .dropdown-menu li a

You are actually targeting three different elements:

  1. An element with the Id of racksTab

  2. An element with the Id of condenserTab

  3. The a element inside the li child of the .dropdown-menu child of the Id element glycolTab

If you intend to select all elements like the third one described, your selector should be:

#racksTab .dropdown-menu li a, #condenserTab .dropdown-menu li a, #glycolTab .dropdown-menu li a



Note: It is possible that you may not need these selectors at all. Consider improving your code with a more appropriate selector as suggested by @j08691 in the comments. However, this suggestion can only be made based on the actual markup. Feel free to provide it so we can assist you further.

Answer №2

Perhaps this is more aligned with your intentions:

$(document).on('click', '#racksTab .dropdown-menu li a, #condenserTab .dropdown-menu li a, #glycolTab .dropdown-menu li a'

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

Display a selection list with an array through jQuery

I want to dynamically populate a dropdown select using an array through jQuery. Check out the code snippet below: // Create a dropdown list with numbers var numbers[] = { 1, 2, 3, 4, 5}; $.each(numbers, function(index, value) { ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

Efficient methods for setting up Leaflet in Nuxt/Vue

I am attempting to integrate leaflet into my Nuxt 3 project without relying on wrappers like @nuxtjs/leaflet. However, I encountered an issue where it says 'Could not find a declaration file for module 'leaflet', despite having installed the ...

What distinguishes res.send from app.post?

I'm just starting to learn about express and HTTP. I've heard that the express library has app.get, app.post, and res.send methods. From what I gather, app.get is used for GET requests and app.post is used for POST requests. How does res.send fit ...

Can one showcase a php content using an ajax event?

I’m having trouble asking the right question because I’m not sure what I’m doing. I have a form that includes a default PHP script that creates three different lines: <form method="GET" name="NewDeliveryNote" action="ItemPosts_INSERT.php"> < ...

When attempting to print using React, inline styles may not be effective

<div styleName="item" key={index} style={{ backgroundColor: color[index] }}> The hex color code stored in color[index] displays correctly in web browsers, but fails to work in print preview mode. Substituting 'blue' for color[index] succe ...

Retrieving data from a popup window with php and javascript

I've been working on creating a pop-up page that will return a value to the parent page and then close. Here is what I have done so far: Main.php: <tr> <th>Project Name</th> <td><input type="text" name="project_name" id="p ...

Mapping an array in ReactJS based on the specific order of another array

In my experience with Unmitigated, the answer proved beneficial. If you're arriving from a Google search, please scroll down for more information. Order: [ "567", "645", "852", "645", "852", "852 ...

What is the best way to assign or convert an object of one type to another specific type?

So here's the scenario: I have an object of type 'any' and I want to assign it an object of type 'myResponse' as shown below. public obj: any; public set Result() { obj = myResponse; } Now, in another function ...

AngularJS's $filter directive and Javascript syntax

Attempted to extract specific data from a JSON file using $routeParams:id. Typically, I would use the following method: var eventId = $routeParams.eventId; $http.get('json/events.json') .success(function(data){ angular.forEach(data,functio ...

Ways to customize the appearance of Bootstrap 4 tooltips

I'm having trouble customizing my Bootstrap 4 tooltip. I want to adjust the background color, height, width, and border, but I can't figure out how to style it to my liking. Here is the HTML code I am using: <i class="fa fa-question-circle" ...

The text manipulates the canvas position and changes the location of mouse hover interaction

I am trying to achieve a similar header layout like the one shown on this page. However, when I insert some text within the header section: <div id="large-header" class="large-header"> <h1 style="font-size:150%;">Dummy Text</h1> ...

Drawing images on a Canvas using specified coordinates: A step-by-step guide

https://i.stack.imgur.com/YkibI.jpg I've been trying to position images on specific coordinates, but I'm having trouble getting the shapes and angles right. Currently, only the top left corner is matching correctly. var _width, _height; var im ...

Ensuring that the image perfectly fills the entire screen on an iPhone X using React Native

Looking for a solution to make my image fit the entire screen on the iPhone X simulator. I've tried adjusting the imageContainer width to "100%" and the container that encompasses everything, but haven't had any luck. Would appreciate any suggest ...

Contrasting Template Helper and Template Variable in Meteor.js

What sets apart the use of a Template Helper from a Template Variable (if that's not the right term)? How do you determine when to utilize each one? In the following example, both Template.apple.price function and the quantity function in Template.ap ...

Tips for positioning elements with a background on a mobile-friendly website

Currently, I am working on a responsive website that contains various graphical elements. One specific element includes a series of markers that are displayed along a pathway within the design. The amount of markers on the pathway can range anywhere from 1 ...

Pressing the tab key makes all placeholders vanish

case 'input': echo '<div class="col-md-3"> <div class="placeholder"> <img src="images/person.png" /> &l ...

What could be causing my Bootstrap 5 hover effect to malfunction?

Can anyone help with changing the hover effect of a bootstrap button in my code? Here's the HTML: <button type="button" class=" btn btn-dark">Sign Up</button> And here's the CSS I tried: .btn-dark:hover { color: ...

Updating component (`App`) during the rendering of another component is not allowed

I have encountered an issue with a react component that I am struggling to resolve. It involves a radial knob control, similar to a slider, and I am trying to achieve two main objectives: Adjust the knob and pass its value up to the parent component for ...

How can we restart jquery if both functions fail to execute within 5 seconds?

I'm in the process of incorporating a timeout or reset into this section of jQuery code. <script type="text/javascript> var $msgNav = $('.nav4'); $msgNav.on('click', handler); function handler() { $('.drop_down_ ...