I have a very basic question but I couldn't find the answer anywhere maybe because I don't know what to search for exactly.
I inserted several read more buttons on my page. By clicking one, the read more text appears, the read more button disappears and a read less button appears.
To do this, this JQuery code works fine for me:
$('#readmore1').click(function(){
$("#readmoretext1").toggle();
$("#readmore1").hide();
$("#readless1").show();
});
$('#readless1').click(function(){
$("#readmoretext1").toggle();
$("#readmore1").show();
$("#readless1").hide();
});
$('#readmore2').click(function(){
$("#readmoretext2").toggle();
$("#readmore2").hide();
$("#readless2").show();
});
$('#readless2').click(function(){
$("#readmoretext2").toggle();
$("#readmore2").show();
$("#readless2").hide();
});
Since I do have some more buttons I wanted to do a loop so I don't have to copy the code everytime. The problem is that I (as far as I can see it) cannot use classes and the .each() function because every read more button has its own unique text. Is there any form of "for-loop" I could use to do the trick?
Thanks in advance! Cheers Peter
readmoreandreadlessinto a single toggle. The exact details of how to best achieve such a function depends on what your DOM looks like.data-attributes could always be used, but knowledge of the DOM may reveal more convenient paths.