Solved

How can I toggle text & link on click

Posted on 2012-04-11
6
294 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

746 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

9 Experts available now in Live!

Get 1:1 Help Now