When hovering over text in HTML, the image remains unchanged in CSS

Trying to modify the displayed image based on the user's click on different text, my code is structured as follows:

$("#zubi a").hover(function() {
  $("#pic").removeClass().addClass($(this).attr('rel'));
});
#pic.p1 {
  content: url("https://picsum.photos/id/236/200/300");
}

#pic.p2 {
  content: url("https://picsum.photos/id/237/200/300");
}

#pic.p3 {
  content: url("https://picsum.photos/id/238/200/300");
}

#pic.p4 {
  content: url("https://picsum.photos/id/239/200/300");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="zubi" class="row">
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a class="dropdown-item" href="category.html">MEN'S WEAR</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a rel="p1" class="dropdown-item" href="category.html">WOMEN'S WEAR</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a rel="p2" class="dropdown-item" href="category.html">KID'S WEAR</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a rel="p3" class="dropdown-item" href="category.html">JEWELLERY</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a rel="p4" class="dropdown-item" href="category.html">OTHER</a></div>
  <div class="col-md-0 col-lg-2"><img id="pic" style="margin-left:37%;height:90px;" class="mw-100 mh-100" src="https://picsum.photos/id/235/200/300" alt="pic" /></div>
</div>

Upon hovering over the text, the image does not change. Can someone please point out what might be incorrect here? Thank you in advance.

Answer №1

jQuery alone cannot cause a site to become "misaligned", unless there are old or inactive jQuery scripts conflicting with newer ones.

If your template already includes jQuery, changing images should not affect the functionality of the code.

Try wrapping the following snippet:

$(function() {
  $("#zubi a").hover(function() {
    $("#pic").removeClass().addClass($(this).attr('rel'));
  });
});

If that doesn't work, here is the equivalent code in vanilla JavaScript with data-rel instead of rel:

document.getElementById("zubi").addEventListener("mouseover", function(e) {
  const tgt = e.target;
  if (tgt.tagName.toUpperCase() === "A") {
    const pic = document.getElementById("pic")
    pic.className="";
    pic.classList.add(tgt.dataset.rel);
  }
});
#pic.p1 {
  content: url("https://picsum.photos/id/236/200/300");
}

#pic.p2 {
  content: url("https://picsum.photos/id/237/200/300");
}

#pic.p3 {
  content: url("https://picsum.photos/id/238/200/300");
}

#pic.p4 {
  content: url("https://picsum.photos/id/239/200/300");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="zubi" class="row">
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a class="dropdown-item" href="category.html">MEN'S WEAR</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a data-rel="p1" class="dropdown-item" href="category.html">WOMEN'S WEAR</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a data-rel="p2" class="dropdown-item" href="category.html">KID'S WEAR</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a data-rel="p3" class="dropdown-item" href="category.html">JEWELLERY</a></div>
  <div class="col-md-4 col-lg-2 mt-3 pb-3"><a data-rel="p4" class="dropdown-item" href="category.html">OTHER</a></div>
  <div class="col-md-0 col-lg-2"><img id="pic" style="margin-left:37%;height:90px;" class="mw-100 mh-100" src="https://picsum.photos/id/235/200/300" alt="pic" /></div>
</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

Refresh sorting in jQuery DataTable

I have encountered a situation where I am trying to accomplish a task that is similar to the one described in this post. Currently, I have a functional jquery dataTable. My goal is to create a function that can reset the sorting to its default state befo ...

Instructions for utilizing a non-string reference without triggering flow errors

I've noticed that string refs are considered legacy in React. You can read more about it here: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md However, I am encountering a flow error in my component and I&apo ...

Combining two sets of elements in Java to form a Json using Jackson

Is there a way to combine two List of objects retrieved from the database into a single object in order to serialize with Jackson and deserialize in the view? ObjectMapper mapper = new ObjectMapper(); jsonTutorias = mapper.writeValueAsString(tuto ...

Tidy provides HTML that does not conform to standard formatting

When using Tidy to clean and format HTML files to make them compliant with HTML/XHTML standards, I have noticed that the output sometimes contains non-standard attribute values, such as: <table id='abc'>... or <input type='button ...

Issues with the functionality of AngularJS router implementation

I have searched through various resources like Stack Overflow and other blogs, but unfortunately, I haven't been able to fix the routing issue in my AngularJS code. Although there are no error messages, the routing functionality doesn't seem to b ...

When attempting to update my avatar using client.user.setAvatar(), the desired changes fail to take effect

Currently, I am attempting to update the bot avatar with a specific user's avatar, but I seem to be encountering some difficulties. I have attempted the following code: client.users.fetch('userid').then((user) => { client.user.setAva ...

How to optimize form fields in Bootstrap by utilizing the size/maxlength attributes in HTML inputs

When I attempted to utilize html5's form size/maxlength with bootstrap, I encountered an intriguing issue. The .form-control class in bootstrap overrides the size, but if removed, the input loses its styling. Check out the code pen here: http://code ...

The jQuery function is not functioning properly following an Ajax request

Below is the script that I am using to automatically open the next input field with X-Editable: $('#rates .editable').on('hidden', function(e, reason){ if(reason === 'save' || reason === 'nochange') { var $next = ...

Ways to resolve the problem of connecting Provider with my store and efficiently transmitting data to components in my Redux-React application

Encountering an error that reads: Uncaught Error: Could not find "store" in either the context or props of "Connect(WebShop)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(WebShop)". Despite havin ...

What sets apart the "+" and "~" selectors in CSS?

I've tried using both of these selectors, but I'm having trouble distinguishing between them. It appears that they are functioning in the same way. There must be a difference that I'm overlooking. ...

Why is the video background not taking up the entire width of the div element?

I've recently integrated the jquery.videoBG plugin into my project to add a video background. Here's the HTML code: <div id="div_demo"> <div class="videoBG"> <video autoplay="" src="media/snow.mp4" style="position: abso ...

Using React-router-dom's Link component can cause styling inconsistencies with material-ui's AppBar Button

Exploring the use of a material-ui Button with react-router-dom's Link is showcased here: import { Link } from 'react-router-dom' import Button from '@material-ui/core/Button'; <Button component={Link} to="/open-collective"> ...

Encounter the error message "Socket closure detected" upon running JSReport in the background on a RHEL system

I'm encountering an issue with JSReport at www.jsreport.net. When I run npm start --production in the background, everything seems to be working fine. But as soon as I close this session, an error pops up: Error occurred - This socket is closed. Sta ...

The JavaScript setInterval function does not have the ability to automatically repeat the task

I am attempting to use the setInterval function to fetch data from another website every 10 seconds. However, it is only running for the first time and then stopping. setInterval(fetchData("testtest"), 10000); function fetchData(username){ var xht ...

Implement static backgrounds on images within an Angular application

I am new to using Angular 7 and I have hit a roadblock. I need help understanding how to resize images so that either the height is 270 and the width is less than 470, or the width is 470 and the height is less than 270. Once resized, I want to place these ...

Choose the tbody element using a selector that has multiple attributes

When using jQuery, you have the ability to select elements with the multiple attribute selector. I attempted to use this selector to target a tbody element based on the Model, Erstzulassung, and Killometerstand properties, but unfortunately, I couldn' ...

Is it recommended for the browser to download multiple images when utilizing srcset?

Here is my HTML markup: <img srcset="https://unique.cloudinary.com/xxx/image/upload/c_scale,w_300/v1653408278/notforsquares/ll/DSCF4429.jpg 300w, https://unique.cloudinary.com/makingthings/image/upload/c_scale,w_600/v1653408278/notforsquares/ll ...

Accessing Session Objects in Struts2 Using a Different Session Object as a Key

I am fairly new to working with Struts2 and currently, I am faced with the challenge of retrieving a session object using a dynamic session key. Here's how my application flow goes: The user will trigger the action from their browser http://localhos ...

Warning using FetchContent JavaScript

I have been attempting to create an alert for the "save" button after it is clicked, but so far I have not been successful. Perfil.html Profile.html is not the main page. It is part of a dashboard. <li class="nav-item"> <a class="nav-link" ...

Issues with PHP session functionality on the server

I am currently developing a straightforward ticket tracking system using PHP and sessions, but I am encountering some difficulties. Here are the main pages involved in this project: admin.php (allows users to create, edit, and delete tickets) login.php ( ...