javascript - how to show the hidden <dl> elements one by one or all at the same time? -
i trying show hidden <dl>
tags when click "view more". in example have 3 <dl>
elements, more or less.
html
<dl class="dl"> <dt><b>name:</b></dt> <dd>my name</dd> <dt><b>department</b></dt> <dd>my department</dd> <dt><b>email</b></dt> <dd>my email</dd> </dl> <br/> <dl class="dl"> <dt>name:</dt> <dd>my name</dd> <dt>department</dt> <dd>my department</dd> <dt>email</dt> <dd>my email</dd> </dl> <br/> <dl class="dl"> <dt>name:</dt> <dd>my name</dd> <dt>department</dt> <dd>my department</dd> <dt>email</dt> <dd>my email</dd> </dl>
jquery
$(function(){ $("#dl").each(function(){ var $dd = $(this).nextuntil("dt"), $this; $dd.filter(":gt(0)").hide(); if($dd.length > 1){ $dd.last().after($("<a href='#'>view more</a>").click(function(e){ e.preventdefault(); $(this).text($(this).text() == "view more"?"hide more":"view more") .prevall("dt:first").nextuntil("dt").filter(":gt(2):not('a')").toggle() })); } }); });
the problem right when click "view more" nothing happens, in other words, other 2 <dl>
elements don't show up. , happen when click view more other <dl>
show , when click hide more hide 1 hidden before clicking show more
i either way: show rest of <dl>
elements 1 one or display rest of <dl>
elements @ once. or/and hide elements 1 one or @ same time.
here jsfiddle
please check following working code. have modified code bit instead of id using class (as @ricardo pontual stated can't use same id multiple times in document).
html
<dl class="dl"> <dt><b>name:</b></dt> <dd>my name</dd> <dt><b>department</b></dt> <dd>my department</dd> <dt><b>email</b></dt> <dd>my email</dd> </dl> <br/> <dl class="dl"> <dt>name:</dt> <dd>my name</dd> <dt>department</dt> <dd>my department</dd> <dt>email</dt> <dd>my email change1</dd> </dl> <br/> <dl class="dl"> <dt>name:</dt> <dd>my name</dd> <dt>department</dt> <dd>my department</dd> <dt>email</dt> <dd>my email change</dd> </dl>
js
$(function(){ var select = $('.dl'); select.filter(":gt(0)").hide(); if(select.length > 1){ $('.dl:last').after($("<a href='#'>view more</a>").click(function(e){ e.preventdefault(); $(this).text($(this).text() == "view more"?"hide more":"view more").prevall("dl").nextuntil("dt").filter(":gt(0):not('a')").toggle() })); } });
Comments
Post a Comment