Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is wrong with this exapand / hide script?

Posted on 2014-10-06
6
Medium Priority
?
130 Views
Last Modified: 2014-10-06
Here's my Javascript:

<script language="JavaScript">
	function expand(param)
	{
		param.style.display=(param.style.display=="none")?"":"";
	}
</script>

<script language="JavaScript">
	function hide(param)
	{
		param.style.display=(param.style.display=="")?"none":"none";
	}
</script>

Open in new window


...and here's my HTML

<a href="#" onclick="expand(document.g<wbr ></wbr>etElementB<wbr ></wbr>yId('div1'<wbr ></wbr>));  this.innerHTML=''; return false;">read more...</a>

<div id="div1" style="display:none;"><tab<wbr ></wbr>le width="600" border="1" align="center">stuff...</table>

<a href="#" onclick="hide(document.get<wbr ></wbr>ElementByI<wbr ></wbr>d('div1'))<wbr ></wbr>; this.innerHTML='none'; return false;">hide...</a></div>

Open in new window


Everything works as far as revealing the div, but when I go to hide it again, I can't see my "read more" link. It's there when you first click on it, but when you click on "hide," you don't see it anymore.

Where am I blowing it?
0
Comment
Question by:brucegust
[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 17

Expert Comment

by:OriNetworks
ID: 40363681
You are setting this.innerHTML=''; which is removing the text Read More...
0
 

Author Comment

by:brucegust
ID: 40363699
Nuts. OK, how do I fix it? Seems like JavaScript is going to process the click that "hides" my div as something that simultaneously hides my "read more." How do I compensate for that?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40363768
test page : http://jsfiddle.net/s85tfdax/

	function expand(param)
	{
		param.style.display = "block";
	}

	function hide(param)
	{
		param.style.display = "none";
	}

    window.onload = function() {
        document.getElementById("readmore").onclick = function() {
            expand(document.getElementById("div1"));
        }
        document.getElementById("hide").onclick = function() {
            hide(document.getElementById("div1"));
        }
    }
    

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Expert Comment

by:OriNetworks
ID: 40363791
You need to assign an id to both the read more and hide links. Then you can use document.getElementById to show/hide them.
0
 
LVL 17

Assisted Solution

by:OriNetworks
OriNetworks earned 800 total points
ID: 40363809
You can keep your existing javascript and reuse the functions to show/hide the read more link
<a href="#" id="lnkMore" onclick="expand(document.getElementById('div1')); hide(this); return false;">read more...</a>

<div id="div1" style="display:none;">stuff...

<a href="#" id="lnkHide" onclick="hide(document.getElementById('div1')); expand(document.getElementById('lnkMore')); return false;">hide...</a>
</div>

Open in new window

0
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 1200 total points
ID: 40363811
Alternate using toggle show hide
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'none')
          e.style.display = 'block';
       else
          e.style.display = 'none';
    }
//-->
</script>



</head>

<body>
<a href="#" onclick="toggle_visibility('contents');">Show/Hide Stuff</a>
<div id="contents" style="display:none"><table width="600" border="1" align="center">stuff...</table></div>
</body>
</html>

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 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