I have this script
that is changing all the letters of a content when you hover them.
Actually it changes the whole format of the page and glue all the content.
I have been told that the main issue is with this part:
var letters = $('p').text();
and that doing it like that
$("p").each(function() {/*$(this) is the current paragraph here*/});
could fix the issues of duplication and formatting
But I have no clue on how to use it since I'm pretty new to all of that. Thanks a lot for the help.
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()));
});
})
.wrapper {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-gap: 10px;
grid-auto-rows: minmax(100px, auto);
}
.one{
grid-column: 1 /5;
grid-row: 1;
background-color:pink;
}
.two{
grid-column: 5 / 8;
grid-row: 1;
background-color:yellow;
}
.three{
grid-column: 8 / 13;
grid-row: 1;
background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<div class="one"><p>I'm the text</p></div>
<div class="two"><p><a>I'm the link in the page</a>
<a href="http://vimeo.com/" target="_blank" style="color: rgb(82, 19, 197);">vimeo</a><sup>➶</sup>
</p></div>
</div>