Reputation: 485
I want display list first when clicking list element(e.g list 1) it should show up paragraph related to it and when clicking another list element (e.g. list 2), it should again hide previous paragraph related to previous element and display paragraph related to this list element(list 2). The paragraph could be in another div Thank you in advance.
<div>
<ul>
<li class="servicelist">List 1</li>
<p class="servicepara">Paragraph of list 1</p>
<li class="servicelist">List 2</li>
<p class="servicepara">Paragraph of list 2</p>
<li class="servicelist">List 3</li>
<p class="servicepara">Paragraph of list 3</p>
</ul>
</div>
Upvotes: 0
Views: 280
Reputation: 28513
First you need to correct your mark up as li and ul is not closed properly, see below html :
<div>
<ul>
<li>List 1</li>
<p>Paragraph of list 1</p>
<li>List 2</li>
<p>Paragraph of list 2</p>
<li>List 3</li>
<p>Paragraph of list 3</p>
</ul>
</div>
Try this
$(document).ready(function(){
$("p").hide();
$("ul li").click(function(){
$(this).siblings("p").hide();
$(this).next().show();
});
});
Working JSFiddle
As said in updated Question, there are multiple divs. So to target
<li class="servicelist">List 1</li>
<p class="servicepara">Paragraph of list 1</p>
You can use below code :
$(document).ready(function(){
$("p.servicepara").hide();
$("li.servicelist").click(function(){
$(this).siblings("p.servicepara").hide();
$(this).next().show();
});
});
Working Demo for updated code
Upvotes: 1
Reputation: 253318
You can, in many browsers, achieve this with CSS alone by (ab)using <label>
and <input>
elements along with the :checked
selector.
Given the following HTML:
<div>
<ul>
<li><label for="li1">List 1</label><input type="checkbox" id="li1" />
<p>Paragraph of list 1</p>
</li>
<li><label for="li2">List 2</label><input type="checkbox" id="li2" />
<p>Paragraph of list 2</p>
</li>
<li><label for="li3">List 3</label><input type="checkbox" id="li3" />
<p>Paragraph of list 3</p>
</li>
</ul>
</div>
It's possible to show multiple <p>
elements (using <input type="checkbox" />
:
li input[type=checkbox] {
width: 0;
height: 0;
}
li p {
display: none;
}
li input:checked ~ p {
display: block;
}
Or, using <input type="radio" />
, it's possible to show only one (at most) <p>
element:
<div>
<ul>
<li><label for="li1">List 1</label><input name="pToggle" type="radio" id="li1" />
<p>Paragraph of list 1</p>
</li>
<li><label for="li2">List 2</label><input name="pToggle" type="radio" id="li2" />
<p>Paragraph of list 2</p>
</li>
<li><label for="li3">List 3</label><input name="pToggle" type="radio" id="li3" />
<p>Paragraph of list 3</p>
</li>
</ul>
</div>
With the CSS:
li input[type=radio] {
width: 0;
height: 0;
}
li p {
display: none;
}
li input:checked ~ p {
display: block;
}
References:
Upvotes: 1
Reputation: 26969
You need to try using jQuery
HTML
<ul>
<li> List 1 <p>Paragraph of list 1</p> </li>
<li>List 2 <p>Paragraph of list 2</p> </li>
<li>List 3<p>Paragraph of list 3</p></li>
</ul>
Jquery
$("p").hide();
$("li").click(function(){
$("p").hide();
$(this).children("p").show();
});
Upvotes: 2