Swap Text/Characters When Hovered Over

I'm looking to add a fun interactive element to my webpage where the letters in certain text rearrange when a user hovers over it. For instance, hovering over "WORK" would display "OWKR" instead. I have a feeling that some JavaScript is needed for this effect, but I'm still relatively new to using JS. Below you'll find the HTML code snippet:

<div class="header">
<div id="access" role="navigation">
<ul id="nav">
    <li id="work"><a href="work.html" title="Portfolio">WORK</a></li>
    <li id="studio"><a href="studio.html" title="About Us">STUDIO</a></li>
    <li id="contact"><a href="mailto:someone">CONTACT</a></li>
    <li id="news"><a href="news.html" title="Goings Ons">NEWS</a></li>
</ul>
</div>
</div>

I came across a helpful post on Stack Overflow about combining hover effects and replacing text content, which guided me in expanding my own code for the list. However, I believe there may be room for improvement in the cleanliness of my JavaScript code. Additionally, I encountered an issue where the text loses its link attribute on mouseover. Here's the JS code snippet:

$(document).ready(function() {   
   $("#work").hover(
      function () {
        $('#work').text('KROW');
      },
      function () {
        $('#work').text('WORK');
      }
    );
    $("#studio").hover(
      function () {
        $('#studio').text('DUTIOS');
      },
      function () {
        $('#studio').text('STUDIO');
      }
    );
    $("#contact").hover(
      function () {
        $('#contact').text('ANOTTCC');
      },
      function () {
        $('#contact').text('CONTACT');
      }
    );
    $("#news").hover(
      function () {
        $('#news').text('ENSW');
      },
      function () {
        $('#news').text('NEWS');
      }
    );
});

While I acknowledge that my approach might not be perfect, it was the only way I could achieve the desired rearrangement effect.

Feel free to check out the jsfiddle here.

Any guidance or assistance would be greatly appreciated!

Answer №1

You can easily achieve the desired effect by utilizing the css property content. It is straightforward to implement.

content:"YOUR DESIRED TEXT"

Feel free to view my jsfiddle example

Please review it and inform me if you encounter any issues.

Answer №2

Here's another method to add data to anchor links and toggle text on mouseover and mouseout events.

Check out the live demo for this example.

To implement this, simply assign a common class like "jumble-up-text" to all anchor links and attach "mouseover" and "mouseout" events to it.

Here is an example of how you can structure your HTML:

<li><a class="jumble-up-text" href="work.html" title="Portfolio" data-changetxt="KROW">WORK</a></li>

And here is the corresponding Javascript code:

$("a.jumble-up-text").on("mouseover mouseout", function(){
    var $this = $(this);
    var sShowTxt = $this.data('changetxt');
    $this.data('changetxt', $this.text());   
    $this.text(sShowTxt);

});

With this code, the anchor tag text will change on mouseover and revert back on mouseout event.

You can easily reuse this code for other anchor links by assigning the same class and adjusting the value of the "data-changetxt" attribute with the desired text for hover effect.

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

What is the best approach to creating a website for a client who needs to incorporate their own Youtube videos?

Welcome to my first post on this platform! I have a new project where I am creating a website for a client who wants to easily embed their own YouTube links. As a beginner in web development, this is my first paid task and I'm curious about how I can ...

Populate a Dropdown Menu with Directory Content for User Selection of d3.js JSON Data

I have a d3 forced-directed graph that is currently using a static JSON file for data: d3.json("../Data/sample.json", function(error, graph) { //do stuff }); However, I would like to give the user the ability to select the data file from a drop-down ...

Bring your Electronic Catalogue to life with the addition of dynamic HTML content showcasing the latest

I am creating a digital magazine but I am facing the challenge of having to deal with 200 pages in jpg format. To streamline the process, I am looking for a way to use a combination of JavaScript and PHP to avoid manually coding for each of the 200 pages. ...

Trouble with minification in Sencha cmd 5

I've been attempting to compress a Sencha 5 project using Sencha CMD, but I keep experiencing failures. sencha generate app -ext demoApp ./demoApp Then, in an effort to compress the application, I entered the following command: sencha app build ...

The vertical alignment of the button text with SVG is not properly aligned

I'm facing a challenge in vertically centering text in a button while also using SVG. The text is centered properly when the SVG is removed. How can I maintain the SVG size and still achieve vertical text centering? https://i.sstatic.net/PJRtR.png ...

What is the best way to ensure that the state is updated only when the user begins typing in a text

I am currently working on a text editor-related code and my main focus is to update the state of the editor only when the user starts typing in the editor. The state should be updated under the following scenarios: 1. Update the state when the user begin ...

Creating a popup trigger in ReactJS to activate when the browser tab is closed

I am currently working on an enrollment form that requires customer information. If a user fills out half of the form and then attempts to close the tab, I want to trigger a popup giving them the option to save and exit or simply exit. While I have a jQue ...

Unable to modify the text color within a moving button

Working on a school project and implemented an animated button style from w3 schools. However, I'm struggling to change the text color within the button. Being new to HTML, I would greatly appreciate any insights or suggestions on what might be going ...

Using Vuex's v-model to bind to a state field in an object

In my current Vuex state, I have defined a getter named configs, which looks like this: configs: { 1303401: { exampleValue: 'test' } } There is also an input where I bind the exampleValue from the Vuex store's state using v-mo ...

Display values in real-time when the text box is modified

Is there a way to update the total value in a text box based on user input using JavaScript and PHP, or just JavaScript? For example, if item "A" costs $25 and the customer orders 5 of "A", the total should automatically display as $125 when the quantity ...

Tips for subscribing to an Angular Unit Test:

In the process of writing test cases for a component, I first tackled a basic test case to ensure smooth functionality. Initially, I faced Dependency Injection errors and managed to resolve them. The current challenge I am encountering involves mocking API ...

What causes the $_FILES superglobal to consistently appear empty?

Check out my page Take a look at my form: <h2>Upload Photo</h2> <form name="photo" enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post"> Photo <input type="file" name="image" size="30" /> & ...

AngularJS redirection causes the previous template to quickly appear and then disappear

This particular question has been circulating without a satisfactory resolution for some time, so hopefully the information provided here will help clear up any confusion. Essentially, I have an object called resolve attached to my routes, set up like thi ...

Having trouble implementing a circular progress bar with a custom Tailwind CSS library, the desired effect is not being achieved

I am on a mission to create a circular progress indicator. Recently, I stumbled upon the daisyui library and its component called radial progress - The documentation mentions that: "Radial progress requires the use of the --value CSS variable." ...

Bootstrap dropdown feature automatically rearranges all items in my navigation bar

I recently set up a navbar for my website, but I'm struggling to grasp the concept of positioning items with Bootstrap. One issue I encountered is that when I click on the dropdown button (profile image), it causes all the items in my navbar to shift, ...

Establishing a default value for sj:select

As I utilize the struts2-jquery plugin, I am faced with the need to establish a default value for the sj:select drop-down tool. The current functionality of the widget is operational, displaying all relevant values (retrieved from a map, where the value c ...

Page rendering issue: Access to page unavailable

I've been attempting to include a link to my privacy policy in the footer using the following code: <a href="/privacypolicy"> Privacy Policy </a> However, when I view the homepage on my local host and try to access the privacy p ...

The npm install command failed due to a lack of suitable versions for pinkie-promise

I am currently attempting to perform a straightforward "npm install" from one of the repositories mentioned in a tutorial provided here Below is the content of the package.json: { "name": "react-playlist", "version": "1.0.0", "description": "A basi ...

easy method for creating a hyperlink that triggers a "download" pop-up box

Is there a simple and efficient way to have some of my links trigger the 'save file as' prompt (similar to right-clicking) immediately after they are clicked for the first time? ...

Unable to locate the module name loaded using require() function

Within my React file, I am importing a Javascript file like this (I am using Typescript): let FloDialog = require('../public/js/flo-dialog.min'); Afterwards, I declare a property for the dialog class: dialog: FloDialog = null; It is important ...