Combining the value of $(this) to create an identifier name

I am attempting to create a hover effect on an h1 element that triggers the glowing effect on a span element with an id that corresponds to the value of the h1. While I have successfully set up a glowing effect for a sentence, I am struggling to replicate it for the h1 elements. My goal is to hover over an h1 and have a square of the same color glow as a result. Can anyone provide insight into where I might be making a mistake?

$(document).ready(function() {

  $("#verb").mouseenter(function() {
    $(".verb").addClass("hovered");
  });
  $("#verb").mouseleave(function() {
    $(".verb").removeClass("hovered");
  });

  $("#noun").mouseenter(function() {
    $(".noun").addClass("hovered");
  });
  $("#noun").mouseleave(function() {
    $(".noun").removeClass("hovered");
  });


  $("h1").mouseenter(function() {
    $("#".concat($(this).val())).addClass("hovered");
  });


});
._21 {
  color: red !important
}

._106 {
  color: orange !important
}


/*glowing effect*/

#verb {
  color: blue
}

#noun {
  color: blue
}

.hovered {
  transition: text-shadow 0.1s linear;
  text-shadow: 0 0 3px blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>
  <span id="verb">Verbs</span> &nbsp; - &nbsp; <span id="noun">Nouns</span>
</p>

I <span class="verb">like</span> 
to <span class="verb">ride</span> 
my <span class="noun">bicycle</span> 
every <span class="noun">day</span>
.
<br><br>

<h1 class="_21" value="red">
  Red
</h1>
<h1 class="_106" value="orange">
  Orange
</h1>

<p style="font-size: 28px">
  <span class="_21" id="red">■</span>
  <span class="_106" id="orange">■</span>
</p>

Answer №1

When working with strings in JavaScript, it is common to use the + operator for concatenation instead of the .concat() method. While both methods achieve the same result, using + tends to be simpler and more familiar to those reading your code. I myself had even forgotten about the existence of the .concat() method when reviewing your code!

According to the MDN page for .concat(), using + or += also provides better performance, although the difference may not be noticeable in a basic scenario like this.

In response to Patrick and Mikey's feedback, h1 elements do not have a value attribute, but you can utilize data-value as an alternative.

For a straightforward task like toggling a class on and off, consider utilizing Jquery's single-argument version of .hover(), where a single function is provided that handles both mouseenter and mouseleave events.

You can combine these suggestions into a cohesive solution as shown below:

$(document).ready(function() {

  $("#verb").hover( function() {
    $(".verb").toggleClass("hovered");
  });

  $("#noun").hover( function() {
    $(".noun").toggleClass("hovered");
  });

  $("h1").hover( function(){
    $( "#" + $(this).data('value') ).toggleClass("hovered");
  });

});
._21 {
  color: red !important
}

._106 {
  color: orange !important
}


/*glowing effect*/

#verb {
  color: blue
}

#noun {
  color: blue
}

.hovered {
  transition: text-shadow 0.1s linear;
  text-shadow: 0 0 3px blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>
  <span id="verb">Verbs</span> &nbsp; - &nbsp; <span id="noun">Nouns</span>
</p>

I <span class="verb">like</span>
to <span class="verb">ride</span>
my <span class="noun">bicycle</span>
every <span class="noun">day</span>
.
<br><br>

<h1 class="_21" data-value="red">
  Red
</h1>
<h1 class="_106" data-value="orange">
  Orange
</h1>

<p style="font-size: 28px">
  <span class="_21" id="red">■</span>
  <span class="_106" id="orange">■</span>
</p>

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

One method I use to retrieve ajax data and assign it before rendering the view

Despite putting the ajax.get in the created function, I am still unable to retrieve the ap_name value. This issue is related to vue.js created: function () { ajax.get('/envs').then(function (res) { this.apName = res.AP_NAME; ...

A guide on transforming a 1-dimensional array into a 2-dimensional matrix layout using Angular

My query revolves around utilizing Template (HTML) within Angular. I am looking for a way to dynamically display an array of objects without permanently converting it. The array consists of objects. kpi: { value: string; header: string; footer: string }[] ...

Obtain information from the get request route in Node.js

I've been diving into nodejs and databases with the help of an online resource. As part of my learning process, I have been tasked with replicating the code below to fetch data from app.use('/server/profil'); However, I'm encountering ...

What could be causing the top navigation bar's background color to not display properly?

Currently working on a website and trying to implement a top navigation bar. However, encountering an issue where the background-color is not displaying properly and appearing transparent. CSS: body { margin: 0; font-family: "Century Gothic", Cent ...

Prevent Click Event on Angular Mat-Button

One of the challenges I'm facing involves a column with buttons within a mat-table. These buttons need to be enabled or disabled based on a value, which is working as intended. However, a new issue arises when a user clicks on a disabled button, resul ...

instructions for creating a hover effect where one div vanishes when hovering over another div

Is there a way to make the line visible when hovering over my circular div? #line { display: none } <div id='circle'> <div id= 'line'> ...

The CSS for SVG circles breaks in Firefox, causing the browser to strip away the radius property

Currently, I am working on creating a progress ring using SVG and CSS. It is functioning well in Chrome; however, Firefox (61.0.1 (64-bit)) is giving me trouble as it does not display the circle. I attempted to apply the method from this question but did n ...

JavaScript and HTML with Node.js

Exploring the world of HTML, running smoothly with a static IP address 192.168.56.152 using apache on the host computer. <!DOCTYPE html> <html > <head> <title>OnlinePage</title> <meta charset="utf-8"& ...

Aligning elements vertically in Bootstrap 4 with the power of flexbox

Seeking assistance with a frustrating issue in Bootstrap 4: I tried to implement the align-self-start flexbox alignment using the code from this page . However, the example provided on that page does not seem to work. Is there a simpler way to achieve the ...

The CSS outline properties vary between input elements and input elements in focus

I'm encountering an issue with a box and its associated CSS outline style. When the box is focused, it should display a blue outline (which is working fine). However, upon form validation, if there is an error, the .error class is added which should c ...

Tips for customizing the appearance of this div using Material UI's card/box components

Hey there! I am currently using a flexible div to house the h1 and arrow image (you can see it https://i.sstatic.net/OgVBp.png). I was wondering if there is a way to utilize a material UI component to give this "black box" a new look? Here is the jsx code ...

Is there a way to incorporate an MTN Momo or Orange Money payment system into your application if you are not a registered business entity?

Implementing an MTN Momo and Orange Money payment system through their respective APIs is crucial. In addition, I am seeking a dependable and effective method to seamlessly integrate these diverse APIs. During my attempt to incorporate the API via their ...

"Using Sequelize's Op.and and Op.like operators led to an unexpected outcome of producing an empty

I am working on developing a search endpoint using express and sequelize. I noticed an issue where using Op.and in my 'where' object results in an empty object: const where = { [Op.and]: req.query.q.split(" ").map((q) => { ...

The Mongoose query for the id field retrieves both the id and _id values

Within my Mongoose schema, there is a specific field named id which holds a unique identifier for each document. This operates using the same system as the standard _id field as shown below: var JobSchema = new mongoose.Schema({ id: { type:String, requi ...

My code seems to be malfunctioning - why can't I keep the aspect ratio?

UPDATE: Can someone please assist me with maintaining the aspect ratio of a drawn image? I am struggling to achieve this and would appreciate any help. I have been attempting to upload an image, draw it using the imageDraw() function, and fit it within a ...

Display an image on an HTML page based on the TypeScript data in an Ionic Angular application

After retrieving user profile data from the database and storing it in an observable, I am able to access properties such as profileData.username, profileData.msgnumber, and more. When profileData.avatar returns the name of the avatar the user is using, I ...

Utilize Recurly's Node to generate a transaction with stored billing details

I need help creating a transaction using Recurly stored billing information. I am currently using the node-recurly module in my Node.js application. https://github.com/robrighter/node-recurly Below is the code snippet that I have written: recurly.transa ...

Displaying an HTML button above JavaScript code

Hello, I am currently working on a project that involves displaying the Earth and I am in need of assistance with positioning some buttons on the screen. Currently, I am facing an issue where the buttons appear either above or below the JavaScript code. I ...

Having trouble with HTML - JavaScript function not processing responseText?

On my website, there is a button array that displays the position of a robot by reading a text file using a php/ajax combo. The script initially sets all buttons to the same color and changes the color of the button to represent the robot's position. ...

Getting data from a wordpress database using javascript

After setting up a table named inspirationWall in my database with id and Visit_Count as INT, I inserted a row with id=1 and Visit_Count = 2. Now, attempting to fetch the Visit_Count value in WordPress and show it in an alert for testing purposes. I have ...