Show specific div when multiple instances exist

Hi,

I have a list of sections that each contain a hidden div and after each section there is a 'show' button that needs to target that hidden div and display it.
The trouble I am having is targeting just the one div to show when there are multiple instances of the same div on the page.

<section>
	<div class="content">
		<div class="hidden-content">
		</div>
	</div>
</section>
<p class="more">More</p>

<section>
	<div class="content">
		<div class="hidden-content">
		</div>
	</div>
</section>
<p class="more">More</p>

<section>
	<div class="content">
		<div class="hidden-content">
		</div>
	</div>
</section>
<p class="more">More</p>

Open in new window

Al4ddin2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QuinnDexCommented:
all your divs have the same class name and no id
give each div a unique id and use this id to decide on which will show
0
RobOwner (Aidellio)Commented:
As @QuinnDex said
<section>
	<div class="content">
		<div id="content1" class="hidden-content">
		</div>
	</div>
</section>
<p class="more">More</p>

<section>
	<div class="content">
		<div id="content2" class="hidden-content">
		</div>
	</div>
</section>
<p class="more">More</p>

<section>
	<div class="content">
		<div id="content3" class="hidden-content">
		</div>
	</div>
</section>
<p class="more">More</p>

Open in new window


Create a button in each section eg <input type="button" onclick ="showdiv('divID')" />
You would pass content1 for the first section etc

Then in your javascript you target like this
function showdiv(mydivID) {
document.getElementById(mydivID).style.display = "block"; // show the div
// alternatively change or remove the class
document.getElementById(mydivID).className="";
}

Open in new window

0
QuinnDexCommented:
or in jquery like this

<html>
<head>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
		$(document).ready(function(){
		    
		    $(".buttons").click(function () {
		    var divname= this.value;
		      $("#"+divname).show("slow").siblings().hide("slow");
		    });
		
		  });
	</script> 
</head>
<body>


<div id="buttonsDiv"> 

<input type="button" id="button1" class="buttons" value="div1"></input>
<input type="button" id="button2" class="buttons" value="div2"></input>
<input type="button" id="button3" class="buttons" value="div3"></input>
<input type="button" id="button4" class="buttons" value="div4"></input>

</div>
	
<div>

<div id="div1" style="display:none">
Hello World
</div>

<div id="div2" style="display:none">
Test
</div>

<div id="div3" style="display:none">
Another Test
</div>

<div id="div4" style="display:none">
Final Test
</div>


</div>


</body>
</html>

Open in new window

0
Al4ddin2Author Commented:
Is there a way of keeping the source code I have used? Instead of buttons?

Thanks
0
Chris StanyonWebDevCommented:
Keep your source code and the jQuery would look like this:

$('.more').click(function(){
    $(this).prev('section').find('.hidden-content').show();
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.