Is it possible to apply a single jQuery statement to multiple selectors within the same webpage?

It may seem like a minor issue, but I've encountered a small dilemma. I have SVG icons on my webpage that are supposed to reveal a hidden div when hovered over. While the first icon functions as expected, subsequent icons do not show the hidden div. After some investigation, I've found that the problem lies with the jQuery code, as the CSS (cursor: pointer;) still applies to each icon but the hidden div does not display. My question is, can I use multiple jQuery statements like the one below for each of my SVGs?

$(".div-g").hover(
   function() {
     $(this).find(".div-hidden").css("display","block");
   },
   function() {
     $(this).find(".div-hidden").css("display","none");
   }
);

For example, using the same statement but replacing "div" with "div1" and so forth for each SVG. I don't see any reason why this wouldn't work, yet it's not as straightforward as it was with the initial icon. To clarify, each of my SVGs has classes similar to the ones shown below

<rect style="display:none;" class="div-hidden div-hidden-rect" width="34.02" height="34.02"/>
<text style="display:none;" class="div-hidden" x="8" y="10">

and corresponding CSS like this

.div-g:hover {
text-align: center;
cursor: pointer;
}
.div-hidden {
text-anchor: middle;
text-align: center;
font-size: .5rem;
display: inline-block;
position: center;
}
.div-hidden-rect {
fill: $whiteblue;
opacity: .96;
}

where .div-g represents the SVG. So, is the issue stemming from using the same jQuery statement with different selectors, or is there something else at play that I'm overlooking?

Answer №1

Give this a shot:

$('.div-g, .div1').hover(
  function(){
    $('.div-hidden').css({display: 'block'});
  }, function(){
    $('.div-hidden').css({display: 'none'});
  });

Answer №2

When using a class selector in CSS, it will target all elements that share the same class. For example:

<div id="div-1" class="svg-container">
  <rect style="display:none;" class="div-hidden div-hidden-rect" width="34.02" height="34.02"/>
  <text style="display:none;" class="div-hidden" x="8" y="10">
</div>

<div id="div-2" class="svg-container">
  <rect style="display:none;" class="div-hidden div-hidden-rect" width="34.02" height="34.02"/>
  <text style="display:none;" class="div-hidden" x="8" y="10">
</div>

<div id="div-3" class="svg-container">
  <rect style="display:none;" class="div-hidden div-hidden-rect" width="34.02" height="34.02"/>
  <text style="display:none;" class="div-hidden" x="8" y="10">
</div>

Using $(".svg-container") as your selector should work fine to target all these divs.

$(".svg-container").hover(
   function() {
     $(this).find(".div-hidden").css("display","block");
   },
   function() {
     $(this).find(".div-hidden").css("display","none");
   }
);

Styling them individually is also easy, especially if you are using a preprocessor like LESS/SASS:

.svg-container {

    &:hover {
        text-align: center;
        cursor: pointer;
    }

    .div-hidden {
        text-anchor: middle;
        text-align: center;
        font-size: .5rem;
        display: inline-block;
        position: center;
    }

    .div-hidden-rect {
        opacity: .96;
    }
}


#div-1 {
    .div-hidden-rect {
        fill: $whiteblue;
    }
}

#div-2 {
    .div-hidden-rect {
        fill: $somethingelse;
    }
}

#div-3 {
    .div-hidden-rect {
        fill: $somethingelseagain;
    }
}

Answer №3

It appears that the solution you seek is simply

$(this).find(".an-hidden").toggle();

$(".hover-change").hover(function() {
     $(this).find(".an-hidden").toggle();
});
.an-1 {
        fill: #2ea3e4;
      }
.an-g:hover {
text-align: center;
cursor: pointer;
}
.an-hidden {
text-anchor: middle;
text-align: center;
font-size: .5rem;
display: inline-block;
position: center;
}
.an-hidden-rect {
fill: #F8FDF7;
opacity: .96;

}
.an-hidden-text {
fill: #2ea3e4;
}
.an-hidden-smtext {
margin-top: 1rem;
font-size: .15rem;
fill: #2ea3e4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg id="test_icon" data-name="Test Icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34.02 34.02">
  <defs>

  </defs>
  <title>analysis_icon</title>
  <g class="an-g hover-change">
    <rect class="an-1" width="14.02" height="14.02"/>
    <rect style="display:none;" class="an-hidden an-hidden-rect" width="14.02" height="14.02"/>
    <text style="display:none;" class="an-hidden" x="8" y="10">
      <tspan x="50%" y="35%" dy=".1em" class="an-hidden-text">Text Title</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">text</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">more text</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">more more text</tspan>
      </text>-->

  </g>
</svg>
<svg id="test_icon2" data-name="Test Icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34.02 34.02">
  <defs>

  </defs>
  <title>analysis_icon</title>
  <g class="an-b hover-change">
    <rect class="an-1" width="14.02" height="14.02"/>
    <rect style="display:none;" class="an-hidden an-hidden-rect" width="14.02" height="14.02"/>
    <text style="display:none;" class="an-hidden" x="8" y="10">
      <tspan x="50%" y="35%" dy=".1em" class="an-hidden-text">Text Title</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">text</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">more text</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">more more text</tspan>
      </text>-->

  </g>
</svg>
<svg id="test_icon3" data-name="Test Icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34.02 34.02">
  <defs>

  </defs>
  <title>analysis_icon</title>
  <g class="an-d  hover-change">
    <rect class="an-1" width="14.02" height="14.02"/>
    <rect style="display:none;" class="an-hidden an-hidden-rect" width="14.02" height="14.02"/>
    <text style="display:none;" class="an-hidden" x="8" y="10">
      <tspan x="50%" y="35%" dy=".1em" class="an-hidden-text">Text Title</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">text</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">more text</tspan>
      <tspan x="50%" dy="1.9em" class="an-hidden-smtext">more more text</tspan>
      </text>-->

  </g>
</svg>

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

Issue with jQuery: Changing colors does not work on Chrome and Safarirowsers

I'm puzzled as to why the code snippet below only works in Firefox, but not in Chrome and Safari. Can you shed some light on this issue? if ($(this).css("color") == "Fuchsia"){ $(this).css("color","#000000"); } Here is the link in question: If you ...

Can one determine whether an SCSS element is devoid of content?

Can SCSS be used to determine if an element is empty? For instance, I'm interested in assigning a variable based on whether the class has content or not. <div class="defaultClass"> <!-- This div is empty --> </div> Is there a w ...

Sorting tables with jQuery UI sortable() and rowspan功能

Can jQuery UI's sortable() be configured to sort rows based on their "containing" element only? I have a table with rowspanned cells that should only be sorted within their respective spanned columns. var $sortable = $('.nested-sortable tbody&ap ...

Uniform Fixed Width for Material UI Table Cells

Below is a Material UI Table with columns set to a fixed size of 205px each, and the table itself set to 100% width. However, when there isn't enough space available, the columns do not shrink according to the text inside them, remaining stuck at 205p ...

Arrange divs in a stack when one becomes too full

Struggling to find a pure CSS solution for this issue, so seeking advice. Imagine three divs aligned horizontally next to each other. If one div overflows onto the next line, I want them to switch to a vertical stack layout. How can this be achieved? I h ...

Change the display, animate the elements within

Currently working on a landing page with a contentSwitcher and everything is functioning properly. However, I am interested in animating the contents sequentially to add some stylish flair. Take a look at the Test Landing Page for reference. If you observ ...

Maintaining image ratio on the entire screen

I am in search of a way to create a webpage that includes the following elements from top to bottom: Full-screen width image/background-image (maintaining aspect ratio) Navigation bar (aligned at the top right of the image) Some text Another full-screen ...

Personalize a bootstrap theme specifically for the mobile interface

My current template, based on Bootstrap, displays a text along with a button and an image. Using a checkbox, you can choose whether the image appears to the right or left of the text. One issue I'm facing is that in the mobile view, the text and butt ...

How can I alter the color of the menu text when scrolling to the top of a webpage?

As I navigate my website, I encounter a menu with a transparent background at the top that changes to black as I scroll down. On a specific page where the background is white, this presents an issue: when the menu reaches the top, the white background clas ...

Making an Ajax request causes the entire content to be swapped out

I have a form with a submit button. Normally, when the submit action is taken, it returns to a new page and does not update the partial view. I tried using an ajax call in a jQuery live function, but it still replaces the entire content instead of returnin ...

I am having trouble displaying SASS styles in the browser after running webpack with node

In my current project for an online course, I am utilizing sass to style everything. The compilation process completes successfully without any errors, but unfortunately, the browser does not display any of the styles. The styles folder contains five file ...

Arrange the selection options by price using jQuery

Sorting options by price is my goal, but despite trying various codes, I have not been successful! This is the original structure: <select id="product-list"> <option>product 1- [2$]</option> <option>product 2- [4$]< ...

What steps should be followed to effectively incorporate Google Fonts into a Material UI custom theme for typography in a React/TypeScript project

Hey there, I'm currently working on incorporating Google fonts into a custom Material UI theme as the global font. However, I'm facing an issue where the font-weight setting is not being applied. It seems to only display the normal weight of 400. ...

Troubleshooting: ajax functionality experiencing issues despite implementation of async and await keywords

Below is the code that is currently functioning well. $.ajax({ url: '?module=abc&action=xyz', //Ajax events beforeSend: function (e) { // }, success: function (e) { const URL1 = '?module=mno&action=pqr&apo ...

Does anyone know of a JavaScript function that works similarly to clientX and clientY but for tooltips when the mouse moves?

When using plain JavaScript to create a function that moves a tooltip on mouse move, the function works the first time with clientX and clientY, but fails to work when repeated. What could be causing this issue with clientX and clientY? Any suggestions on ...

Error Parsing JSON using jQuery

I am encountering a Parse Error while working with valid JSON, even though I have validated it on jsonlint. The data is fetched from a MySQL database using PHP and converted into a JSON string before being retrieved in jQuery (refer to the code below). B ...

Troubleshooting a JQuery AJAX Autocomplete problem involving PHP and MySQL

I am facing an issue with my autocomplete feature. It is functioning properly on one of my pages, but not on this particular page. Even though the correct number of entries is being retrieved, they all appear to be "blank" or are displayed in black text th ...

What is the best way to adjust a CSS width using the output from a JavaScript function?

I am facing a challenge with a GridView within a div wrapper. The row headers along the left side need to always be visible. So far, this setup is working fine. However, I need the wrapper to have a variable width to adjust to the browser size. Although I ...

Modifying the font style and color of text within the table header - Material Table version 0.19.4

Recently, I began using React and Material UI but have encountered an issue with modifying the color and font of text in the table header. Despite attempting to override it, the default style remains unchanged. It has been mentioned that utilizing "heade ...

How can you customize the color of the arrows in Carousel Bootstrap?

I've utilized Carousel Bootstrap to create a slideshow, but I'm struggling with changing the color of the arrows. Could you please assist me in figuring out how to change the color of the arrows? Thank you! slideshow.component.html: <di ...