So I have a classic "more/less" paragraph here:
/
When I click on "More info" the link reveals some content and at the same time it switches to "Less info" but I only want this to happen to one link at a time, as I will have more paragraphs on my page. Right now when I click on the "More info" link, the other one will switch too, which is what I'm trying to avoid.
Any help appreciated!!!
So I have a classic "more/less" paragraph here:
http://jsfiddle/dVFaV/38/
When I click on "More info" the link reveals some content and at the same time it switches to "Less info" but I only want this to happen to one link at a time, as I will have more paragraphs on my page. Right now when I click on the "More info" link, the other one will switch too, which is what I'm trying to avoid.
Any help appreciated!!!
Share Improve this question edited Oct 10, 2012 at 13:52 Zarkonnen 22.5k14 gold badges68 silver badges81 bronze badges asked Oct 10, 2012 at 13:47 user1735065user1735065 231 silver badge3 bronze badges 5- 3 Give your anchors IDs and operate against that. – aserwin Commented Oct 10, 2012 at 13:51
- The thing is I'm working on Expressionengine and I need one generic code for the "more/less" content so my client can add as many items as he needs and therefore I can't use ID's. – user1735065 Commented Oct 10, 2012 at 13:55
- In that case, if you know how many anchors there will be, you can target specific ones by place in jQuery (.second, .third, etc) - I don't know the exact syntax off the top of my head, but I have seen it done. – aserwin Commented Oct 10, 2012 at 13:58
- The problem is that I don't know how many anchors there will be, my code needs to be generic for multiple entries. But thanks for your help! – user1735065 Commented Oct 10, 2012 at 14:01
-
Like what soju said...
$this
will act on the anchor that was actually clicked! – aserwin Commented Oct 10, 2012 at 14:03
3 Answers
Reset to default 3If your HTML structure stay like this, you can simply use :
function togglePanel() {
var $this = $(this);
if ($this.hasClass('button_open'))
$this.parent().next().slideDown('slow');
else
$this.parent().next().slideUp('slow');
$this.hide().siblings().show();
}
$('.adv-toggle-buttons > a').click(togglePanel);
http://jsfiddle/WFC7s/
EDIT : Or like Bondye said in his answer, with slideToggle :
function togglePanel() {
var $this = $(this);
$this.parent().next().slideToggle('slow');
$this.hide().siblings().show();
}
$('.adv-toggle-buttons > a').click(togglePanel);
Why dont you use toggle() and slideToggle()?
HTML
<p class="adv-toggle-buttons">
<a href="#">[+] More info</a>
<a href="#" style="display: none;">[-] Less info</a>
</p>
<div class="adv-unit-options" style="display: none;">Lorem ipsum</div>
<p class="adv-toggle-buttons">
<a href="#">[+] More info</a>
<a href="#" style="display: none;">[-] Less info</a>
</p>
<div class="adv-unit-options" style="display: none;">Lorem ipsum</div>
jQuery
$('.adv-toggle-buttons a').each(function() {
$(this).on('click', function(){
$(this).parent().next().slideToggle();
$(this).parent().find('a').each(function(){
$(this).toggle();
});
});
});
Fiddle
http://jsfiddle/dVFaV/40/
With the above you would have to keep 2 containers for the content. One for the short "less" version and one for the longer "more" version. I like to keep my content as easy to update as possible and actually ran into a situation where I couldn't do anything but add JS to the content to adjust the dom. I ended up using the same content just striping it down to text before I cut it. This would keep you from cutting any HTML tag and messing the content up.
Your HTML would be something like:
<div class="entry-content">
<div class="event-detail">
<div class="bio">
<p><h2>Lorem 1</h2> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea modo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<div class="event-detail">
<div class="bio">
<p><h2>Lorem 2</h2> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea modo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
The JS would look for each bio and add a more/less button and would also truncate the copy to a set value that works for your layout. The JS should be:
$(document).ready(function(){
var num_of_bios = $('.entry-content .bio').length;
for (i = 0; i < num_of_bios; i++) {
bio_more = $('.bio:eq('+i+')').html();
bio_less = $('.bio:eq('+i+')').text();
bio_less = bio_less.substring(0,200);
$('.bio:eq('+i+')').html(bio_less+' ... ');
$('.bio:eq('+i+')').addClass('bio_less bio_'+i);
$('.bio:eq('+i+')').parent().append('<div class="bio_more bio_'+i+'">'+bio_more+'</div>');
$('.bio_more').hide();
$('.bio:eq('+i+')').parent().append('<div class="bio_more_less" name="bio_'+i+'">more</div>');
}
$('.bio_more_less').click(function(){
show_me = $(this).text();
bio_name = $(this).attr('name');
if( show_me === 'more'){
$('.bio_less.'+bio_name).hide();
$('.bio_more.'+bio_name).show();
$(this).text('less');
}
else if( show_me === 'less'){
$('.bio_less.'+bio_name).show();
$('.bio_more.'+bio_name).hide();
$(this).text('more');
}
});
});
Here is a fiddle. http://jsfiddle/jaredlyvers/pezw77bk/10/