Solved

Show/Hide CSS Toggle with Javascript

Posted on 2011-09-16
6
483 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Hide un-named HTML Label with CSS 14 36
forgetful about js objects 8 46
Start Auto-download of File when page loads? 1 22
2 separate CSS animations 2 17
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

828 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