Solved

How can I toggle text & link on click

Posted on 2012-04-11
6
297 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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 64
jQuery delay animation 3 30
Porbably easy CSS question about overlapping and offset images 21 26
CSS: Making responsive table look nicer 7 27
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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…
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…

831 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