Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I toggle text & link on click

Posted on 2012-04-11
6
Medium Priority
?
302 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 1000 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
Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

 

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 1000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will learn how to dynamically set the form action using jQuery.
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

722 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