Hello, I am brand new to utilizing javascript and I'm currently experimenting with the toggle function. My goal is to create show/hide functionality while also toggling arrow icons. Specifically, I want a right arrow next to the link "More -->" and when clicked, it should change to "Less ^" with an up arrow. I am using icomoon fonts for these arrow icons.
Here is my current code. The show/hide functionality works fine, but I believe there might be a more efficient way to handle the arrow toggling. Initially, the arrow displays correctly, however, upon clicking the link, the arrow disappears and the HTML code becomes visible instead of the arrow icon. Unfortunately, I cannot share a direct link as I am working in a secure sandbox environment:
// Define text for show/hide links
var showText = 'More' + '<span class="icon-arrow-right2"></span>';
var hideText = 'Less' + '<span class="icon-arrow-up2"></span>';
// Append show/hide links to the element preceding the one with "toggle" class
$(".toggle").prev().append(' (<a href="#" class="toggleLink">' + showText + '</a>)');
// Hide all elements with the 'toggle' class
$('.toggle').hide();
$('a.toggleLink').click(function() {
// Toggle link text based on visibility
if ($(this).text() == showText) {
$(this).text(hideText);
$(this).parent().next('.toggle').slideDown('fast');
} else {
$(this).text(showText);
$(this).parent().next('.toggle').slideUp('fast');
}
Below is a snippet of my CSS:
/* Main toggle */
.toggle {
font-size: 14px;
line-height: 20px;
font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
background: #ffffff;
margin-bottom: 10px;
padding: 5px;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
/* Adding a right arrow to the More text */
.icon-arrow-right2:before {
content: "\ea3c";
}
/* Adding an Up arrow to Less text */
.icon-arrow-up2:before {
content: "\ea3a";
}
Thank you for taking the time to review and provide feedback!