Reputation: 5
What I want to do is to have all paragraphs but #p0 hidden on load. Additionally I want to show a paragraph if user clicks on it's sibling span, and hide all other paragraphs
<ul>
<li>
<h1>
<span id="span0">Lorem</span>
<p id="p0">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span1">Lorem2</span>
<p id="p1">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span2">Lorem3</span>
<p id="p2">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
...
Upvotes: 0
Views: 59
Reputation: 36642
$('p:not("#p0")').hide();
$('span').on('click',function() {
$('p').hide();
$(this).next('p').show();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>
<h1>
<span id="span0">Lorem</span>
<p id="p0">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span1">Lorem2</span>
<p id="p1">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span2">Lorem3</span>
<p id="p2">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
</ul>
If you wish to isolate this from other p
's / span
's on the page you could use the 'prefixed-by' attribute selector (^=
) ...
$('p[id^="p"]:not("#p0")').hide();
$('span[id^="span"]').on('click',function() {
$('p[id^="p"]').hide();
$(this).next('p[id^="p"]').show();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>
<h1>
<span id="span0">Lorem</span>
<p id="p0">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span1">Lorem2</span>
<p id="p1">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span2">Lorem3</span>
<p id="p2">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
</ul>
Upvotes: 1
Reputation: 6909
<ul>
<li>
<h1>
<span id="span0" class="spanclick">Lorem</span>
<p id="p0" class="par1">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span1" class="spanclick">Lorem2</span>
<p id="p1" class="par1">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<li>
<h1>
<span id="span2" class="spanclick">Lorem3</span>
<p id="p2" class="par1">Lorem ipsum dolor sit amet,</p>
</h1>
</li>
<script>
$(document).ready(function () {
$(".par1").hide();
$(".spanclick").click(function(){
$(".par1").toggle();
});
});
Upvotes: 0
Reputation: 3760
$('#p0').hide();
$('span').on('click',function() {
$('p').hide();
$(this).next('p').show();
});
Upvotes: 0