Solved

Show/Hide CSS Toggle with Javascript

Posted on 2011-09-16
6
476 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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... …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

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

13 Experts available now in Live!

Get 1:1 Help Now