Solved

Show / Hide JQuery Script (Provided, working Example)

Posted on 2013-01-30
4
230 Views
Last Modified: 2013-02-02
Hello All;

I found this script from:
www.designing4u.de (Show/Hide)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){  
  $('.ahref',this).toggle(
    function () {
      var id = $(this).attr('href');
      if($('.box'+id).text() == '') {  
       $(".box"+item.id).html(html).slideDown(1000); 
        $(this).html("hide");
      } else { 
        $(this).html('hide');  
        $('.box'+id).slideDown(1000);  
      }  
    },  
    function () {  
      var id = $(this).attr('href');
      $(this).html('show');  
      $('.box'+id).slideUp(1000);  
    }  
  );
});
</script>
</head>

<body>
<a href="1" class="ahref">show 1</a>
<a href="2" class="ahref">show 2</a>
<a href="3" class="ahref">show 3</a>
<div class="box1">1</div> 
<div class="box2">2</div> 
<div class="box3">3</div>
<div style="clear:left"></div>
</body>
</html>

Open in new window


OK, I LOVE this code, and it WORKS in my project, so I want to use it so badly, I can taste it.
Now, my question is the following.

When you run the code, the following happened.

Step #1: You have to click the
show 1
show 2
show 3

Step #2: Then you have to click the
show
show
show

Step #3: Then the layers will hide themselves.
So, this is an unneeded step, that I would prefer not to have.

Can someone please let me know what needs to be done in order to eliminate this extra step.

Thank you
Carrzkiss
0
Comment
Question by:Wayne Barron
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
selvol earned 500 total points
ID: 38838700
Try this my Friend....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){  
  $('.ahref',this).toggle(
    function () {
      var id = $(this).attr('href');
      if($('.box'+id).text() == '') {  
       $(".box"+item.id).html(html).slideUp(1000); 
        $(this).html("hide");
      } else { 
        $(this).html('hide');  
        $('.box'+id).slideUp(1000);  
      }  
    },  
    function () {  
      var id = $(this).attr('href');
      $(this).html('show');  
      $('.box'+id).slideDown(1000);  
    }  
  );
});
</script>
</head>

<body>
<a href="1" class="ahref">show 1</a>
<a href="2" class="ahref">show 2</a>
<a href="3" class="ahref">show 3</a>
<div class="box1">1</div> 
<div class="box2">2</div> 
<div class="box3">3</div>
<div style="clear:left"></div>
</body>
</html>
                                  

Open in new window


I think I got the question.

Selvol
0
 
LVL 30

Author Closing Comment

by:Wayne Barron
ID: 38838717
instead of just coping and pasting, I looked through the code.
Took my 2 glances, to catch on to the SlideUp and SlideDown, being changed around.

I wonder why the developer missed that one?

However, you rock "Selvol"
Carrzkiss
0
 
LVL 17

Expert Comment

by:selvol
ID: 38838981
Don't be afraid to experiment with code.
That is just what I did.

I am glad to see you actually looked at the code and figured out what
was changed.


That in its self is really what I am here for (on EE).
I would rather answer questions knowing that the asker learns from it
and not just copies and pastes.......

Glad I could Help......

Selvol
0
 
LVL 30

Author Comment

by:Wayne Barron
ID: 38847029
You are not kidding there Selvol.

I helped this one person here a few years ago, and found out that all he did
Was copy and paste, and never paid one bit of attention to the code itself.
And then he started coming in and asking the same questions, over and over again.
It got sickening.
So I caught him on it, and made an example of him.
Then everyone that was helping him, started getting him to try different things
before they would assist him further.

I want to learn more, I want my knowledge to extend further than what it is.

Have a good weekend Selvol
Carrzkiss
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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

867 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

21 Experts available now in Live!

Get 1:1 Help Now