Solved

How can I toggle text & link on click

Posted on 2012-04-11
6
295 Views
Last Modified: 2012-04-12
I have the following code that expands and collapses a div when clicked. I would like to know how to toggle the text from "Show More" to "Show Less" when clicked. Also, when the "Show Less" is displayed how to also make "Show Less" an actual link.

<html>
<head>
<title>Hide/Show Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
	$(".msg_body").hide();
	$(".msg_head").click(function(){
		$(this).next(".msg_body").slideToggle(550);
	});
});
</script>


</head>
<body>

<div class="msg_head" style="cursor:pointer">Show More</div>
<div class="msg_body">
	<p>This is a test.</p>
	<p>This is a test.</p>
	<p>This is a test.</p>
</div>

</body>
</html>

Open in new window


Thanks for the help!
0
Comment
Question by:greddin
  • 3
  • 2
6 Comments
 
LVL 30

Assisted Solution

by:LZ1
LZ1 earned 250 total points
ID: 37833707
Try this:
<html>
<head>
<title>Hide/Show Test</title>
<style type="text/css">
	.msg_head{ cursor:pointer;border-bottom:1px solid #000;width:85px;text-align:center;}
	.msg_body{float:left;clear:both;border:1px solid #36f;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
	$(".msg_body").hide();
	$(".msg_head").click(function(){
		$(this).next(".msg_body").slideToggle(550);
		$(this).text($(this).text() == 'Show More' ? 'Show Less' : 'Show More');	
	});
});
</script>


</head>
<body>

<div class="msg_head" style="cursor:pointer">Show More</div>
<div class="msg_body">
	<p>This is a test.</p>
	<p>This is a test.</p>
	<p>This is a test.</p>
</div>

</body>
</html>

Open in new window

0
 

Author Comment

by:greddin
ID: 37834059
Cool, that changes the text perfectly as I wanted. But is there any way to add a link to it when the text displays at "Show Less".

Ideally, I want to call another Javascript function when the text "Show Less" is clicked, as well as hide the div as it does now.
0
 
LVL 30

Expert Comment

by:LZ1
ID: 37834077
Not sure what you mean by add a link?  You can just enclose it in an anchor tag, then add your next function below.

<script type="text/javascript">
$(document).ready(function(){
	$(".msg_body").hide();
	$(".msg_head").click(function(){
		$(this).next(".msg_body").slideToggle(550);
		$(this).text($(this).text() == 'Show More' ? 'Show Less' : 'Show More');	
	});
//Next function goes here
});
</script>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:greddin
ID: 37834188
For example, what if I wanted to call this function at the same time the "Show Less" is clicked


function testFunction() {
	alert("This was called by testFunction.");
}

Open in new window


"Show More" just expands the div.

"Show Less" closes the div and calls the extra function.

I hope that makes sense.
0
 

Author Comment

by:greddin
ID: 37834820
I've put my test function call in there, but it's getting called each time the text is toggled. How can I call the testFunction() only when the text was "Show Less"?

<html>
<head>
<title>Hide/Show Test</title>
<style type="text/css">
	.msg_head{ cursor:pointer;border-bottom:1px solid #000;width:85px;text-align:center;}
	.msg_body{float:left;clear:both;border:1px solid #36f;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
	$(".msg_body").hide();
	$(".msg_head").click(function(){
		$(this).next(".msg_body").slideToggle(550);
		$(this).text($(this).text() == 'Show More' ? 'Show Less' : 'Show More');
		testFunction();
	});
});


function testFunction() {
	alert("This should appear only when Show Less was clicked.");
}

</script>


</head>
<body>

<div class="msg_head" style="cursor:pointer">Show More</div>
<div class="msg_body">
	<p>This is a test.</p>
	<p>This is a test.</p>
	<p>This is a test.</p>
</div>

</body>
</html>

Open in new window

0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 250 total points
ID: 37836698
$(document).ready(function(){
	$(".msg_body").hide();
	$(".msg_head").click(function(){
		$(this).next(".msg_body").slideToggle(550);
		if($(this).text() == 'Show More')
		{
			$(this).text('Show Less');
		}
		else
		{
			$(this).text('Show More');
			testFunction();
		}
	});
});


function testFunction() {
	alert("This should appear only when Show Less was clicked.");
}

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now