Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Show/Hide CSS Toggle with Javascript

Posted on 2011-09-16
6
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 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…

610 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