1

I'm looking for a way to change the letters in a text when you hover them. After that, they will eventually not got back to normal and stay changed. So I found a code to change the color of each letter of a text, but I'm looking for the same thing that picks the next leter in the alphabet.

Thanks a lot for the help.

$(document).ready(function(){
  var letters = $('p').text();
  var nHTML = '';
  for(var letter of letters) {
    nHTML+="<span class='x'>"+letter+"</span>";
  }
  $('p').html(nHTML);
})
.x:hover {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p>Hello World!</p>

I also found this code that let you change a text with an id. I just don't know how to apply that to all my characters individually.

const $foo = $('#foo');

$foo.hover(function(){
  $foo.text('Blah');
}, function(){
  $foo.text('Foo');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
  <li><a href="#0" id="foo">Foo</a></li>
</nav>

Also, I have this script that can find the next letter.

function LetterChanges(str) {
    var result = "";
    for (var i = 0; i < str.length; i++) {
        if (122 == str.charCodeAt(i)) {
            result += "a";
        } else if (90 == str.charCodeAt(i)) {
            result += "A";
        } else if ((65 <= str.charCodeAt(i) && str.charCodeAt(i) <= 89) || (97 <= str.charCodeAt(i) && str.charCodeAt(i) <= 121)) {
            result += String.fromCharCode(str.charCodeAt(i) + 1);
        } else {
            result += str.charAt(i);
        }
    }
    return result;
}

console.log(LetterChanges("AaZz+cod!foo"));
$('#result').html(LetterChanges("paul & rémi"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 id="result"></h1>
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Aurore
  • 696
  • 4
  • 16

1 Answers1

1

If I understand what you need, then this is the solution:

function nextLetter(ch) {
    if (!ch.match(/[a-z]/i)) return ch;
    else if (ch === 'Z') return 'A';
    else if (ch === 'z') return 'a';
    return String.fromCharCode(ch.charCodeAt(0) + 1);
}
$(document).ready(function(){
  var letters = $('p').text();
  var nHTML = '';
  for(var letter of letters) {
    nHTML+="<span class='x'>"+letter+"</span>";
  }
  $('p').html(nHTML);
  $(".x").hover(function(e) {
    if (e.type === "mouseenter") $(this).text(nextLetter($(this).text()));
  });
})

Explanation:

  • nextLetter
    • checks whether it's a letter and does nothing if it's not a letter
    • if it's a Z or z, then will start over the alphabet, keeping the case
    • otherwise finds the next letter
  • the for cycle creates spans containing single letters, having the class of x
  • this is why the CSS hover rule works
  • we can create Javascript/jQuery events
  • I have created a Javascript event which changes the letter to the next one on hover

Fiddle: https://jsfiddle.net/1ar8uL4s/

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • That is perfectly what I needed. Thanks a lot. I understand it better now. I still have one question: what if I want the punctuation and the accented letters to change in a certain order too? Thanks a lot again. – Aurore Jun 28 '20 at 19:56
  • 1
    @paulb you are welcome. You can enhance nextLetter according to your exact needs. You will likely have more cases in nextLetter and change the conditions there a bit. If you have trouble doing it, then you can ask a separate question. I encourage you to try to solve that to get some practice, but if you encounter another blocker, you might want to ask another question and leave its link here in the comment section. – Lajos Arpad Jun 28 '20 at 20:17
  • Hey thanks again, I seemed to make it work, but somehow it duplicate the content of

    and also seem to reformat everything... I admit I'm quite stuck. https://codepen.io/pbouigue/pen/WNrZjdp Here is the pen so you can see how it duplicates and removes the styling and links thanks a lot again.

    – Aurore Jun 29 '20 at 09:59
  • @paulb The comment section here is not the appropriate place to fix this. I encourage you to ask another question and let me know about it here in the comment section. I will definitely look into it either today or tomorrow. But, to provide you some ideas which might help you find the solution, the main problem I see at the first glance is ```var letters = $('p').text();``` This takes all text from all the paragraphs. This is what glues your values together (the duplication). And then ```$('p').html(nHTML);``` puts the glued text into all paragraphs. – Lajos Arpad Jun 29 '20 at 10:51
  • @paulb You will need to do it like ```$("p").each(function() {/*$(this) is the current paragraph here*/});```, basically treat the tags as individuals, separate their content from each-other and to achieve that you will need to wrap a loop around the logic you have implemented. Hopefully this will help you solve it, but if there are still blockers, then I kindly ask you to ask another question. – Lajos Arpad Jun 29 '20 at 10:53
  • Thanks again for the support, and sorry for posting it there. I seem to struggle a lot since i'm new to this. I asked another question. – Aurore Jun 29 '20 at 11:48
  • [Changing the letters of a word reformat the whole html](https://stackoverflow.com/questions/62637453/changing-the-letters-of-a-word-reformat-the-whole-html/62637647?noredirect=1#comment110772577_62637647) here is another topic I created. Thanks again for the help. – Aurore Jun 29 '20 at 13:23
  • @paulb don't be sorry, every developer has to start at some point and everything is difficult before knowledge is gained about it. Looking into the issue. – Lajos Arpad Jun 30 '20 at 08:46