Solved

Show/Hide CSS Toggle with Javascript

Posted on 2011-09-16
6
477 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

863 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

24 Experts available now in Live!

Get 1:1 Help Now