Solved

Show/Hide CSS Toggle with Javascript

Posted on 2011-09-16
6
479 Views
Last Modified: 2012-08-14
I'm wanting to use javascript to toggle css elements to show/hide text when someone clicks "read more" and then hides the text when they click "show less". I've tried a few ways of doing this and none of my attempts are working.

<style>
.show { display: block; visibility: visible; }
.hide { display: none; visibility: hidden; }
</style>
<script>
function showit(id) {
	document.getElementById('id').className.remove('hide');
	document.getElementById('id').className.add('show');
}

function hideit(id) {
	document.getElementById('id').className.remove('show');
	document.getElementById('id').className.add('hide');
}
</script>

<div>This is generic start of a paragraph <span id="show1" class="show"><a href="#" onclick="showit('readthis1');hideit('show1');">(read more...)</a></span><span id="readthis1" class="hide"> which continues down here and starts off hidden but then shows up when you click the read more link.<br /><a href="#" onclick="hideit('readthis1');showit('show1');>(show less...)</a></div>

Open in new window


Thanks!
Aus2Srq
0
Comment
Question by:Aus2Srq
6 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36550533
copy & paste:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<style>
.show { display: block; visibility: visible; }
.hide { display: none; visibility: hidden; }
</style>
<script>
function showit(id) {
	var e=document.getElementById(id);
	e.className = e.className.replace(/\bhide\b/,'');
	e.className = e.className + ' show';
}

function hideit(id) {
	var e=document.getElementById(id);
	e.className=e.className.replace(/\bshow\b/,'');
	e.className=e.className + ' hide';
}
</script>

<div>This is generic start of a paragraph <span id="show1" class="show"><a href="#" onclick="showit('readthis1');hideit('show1');">(read more...)</a></span><span id="readthis1" class="hide"> which continues down here and starts off hidden but then shows up when you click the read more link.<br /><a href="#" onclick="hideit('readthis1');showit('show1');">(show less...)</a></div>
</body>
</html>

Open in new window

0
 
LVL 4

Accepted Solution

by:
GeoffHarper earned 500 total points
ID: 36550541
Try this:
<style>
.show { display: block; visibility: visible; }
.hide { display: none; visibility: hidden; }
</style>
<script>
function showit(id) {
	document.getElementById(id).className = 'show';
}

function hideit(id) {
	document.getElementById(id).className = 'hide';
}
</script>

<div>This is generic start of a paragraph <span id="show1" class="show"><a href="#" onclick="showit('readthis1');hideit('show1');">(read more...)</a></span><span id="readthis1" class="hide"> which continues down here and starts off hidden but then shows up when you click the read more link.<br /><a href="#" onclick="hideit('readthis1');showit('show1');">(show less...)</a></div>

Open in new window

0
 
LVL 16

Expert Comment

by:SSupreme
ID: 36550584
or
function showit(parametr){
  var element=document.getElementById(parametr);
  if(!element)return true;
  if(element.style.display=="none"){
    element.style.display="block"
  } else {
    element.style.display="none"
  }
  return true;
}

Open in new window

<a type="button" class="button" onclick="return showit('readthis1')" >(read more...)</a>

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Closing Comment

by:Aus2Srq
ID: 36551164
Simple and works well.

Thanks!

Aus2Srq
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 36551268
I like jquery, cause it's just so easy. I wrote this little accordion recently and you are welcome to view source. It's all there. CSS at the top, JS at the bottom. Make sure you include the jquery lib.

Degrades gracefully for no JS, plus it's non intrusive - all js separate from the html - which is nice.

http://www.candpgeneration.com/EE-showHide.php

Cheers.
0
 
LVL 18

Expert Comment

by:nap0leon
ID: 36551364
I see you already accepted a solution, but wanted to offer this to you as well - a single function that toggle between "hide" and "show".  If the DIV is hidden, it will show it.  If the DIV is shown, it will hide it.

works on DIV such as:
<div id="one"><p>DIV ID=one</p></div>

implement in a link as:
<a href="javascript:safeToggleFieldDisplay(document.getElementById('one'),'flip');">Show/Hide DIV ID=one</a>

Here's a working sample:
http://www.jasondahlin.com/WebSite2/CodeSnippets/JavaScript-HideShowDivs.aspx

function safeToggleFieldDisplay(field, sVisibility){
  try{
    if((field) && (field.style)){
      if (sVisibility=='flip'){
        if (field.style.display == 'none'){
          sVisibility = 'block'; }
        else {
          sVisibility = 'none'; }
      }
      field.style.display = sVisibility;
    }
  }
  catch(exception){
    //no handling - just preventing page explosions
  }
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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... …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

786 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