Solved

How can I toggle text & link on click

Posted on 2012-04-11
6
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

634 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