html - Unique collapsible text with single jQuery function -


i'm using jquery first time , i'm trying track down best way show/hide portions of text multiple times throughout page single function. have around 30 "newsletters" different staff members , want show first paragraph of each option toggle rest of each letter. here's have worked out -

jquery:

$(function() { $('#toggle').click(function() {     $('.toggle').toggle();     return false; }); });  $(function(){    $(".collapse").click(function () {    $(this).text(function(i, text){       return text === "less" ? "more" : "less";       })   }); }) 

html:

preview text here , <span class="toggle" style="display:none;">hidden text here</span> <a href="#" id="toggle" class="collapse">more</a> 

i need repeat each newsletter preview don't want each newsletter need call unique function.

i'm sure super simple fix, have absolutely 0 experience. in advance!

use collapse class , $.siblings():

<p>     preview text here , <span class="toggle" style="display:none;">hidden text here</span>     <a href="#" class="collapse">more</a> </p>  $(function () {     $('.collapse').click(function () {         $(this)             .siblings('.toggle').toggle().end()             .text(function (i, text) {                 return text === "less" ? "more" : "less";             });          return false;     }); }); 

http://jsfiddle.net/cpuqg/1


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -

jquery - Keeping Kendo Datepicker in min/max range -