Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2013, 09:31 AM   PM User | #1
rexhvn
New Coder

 
Join Date: Oct 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
rexhvn is an unknown quantity at this point
Multiple toggles displaying in same area

Hi everyone...

I'm hoping to get some assistance.

I'm creating multiple search functions... lets call them as per below

Search: 1, 2, 3 and 4

All the searches are to appear in the same area just different design.

Currently my javascript is not setup correctly and the way I want it. When I click on 1, all is good. However when I click 2, it won't appear as search 1 is appearing.

My objective is to ensure that only the search function clicked on will appear, and the rest will remain hidden. IE: If 1 is clicked... appear. If 2 is then clicked, 1 should disappear and 2 appear.

This is my html code
Code:
  <div id="buyrevealed" style="display: none;">
more code for search funcion
</div>
java
Code:
function togglebuy( )

{
    var div = document.getElementById("buyrevealed");

    div.style.display = ( div.style.display == "block" ) ? "none" : "block";
}
     </script>
I have the same javascript for all 4 searches to appear.

Would love some assistance. Hope this makes sense.

Thank you.
rexhvn is offline   Reply With Quote
Old 01-25-2013, 10:07 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Do you mean you have EXACTLY the same code for all search elements? You should make sure to change the ID attribute, because each ID attribute can only appear once per document.
devnull69 is offline   Reply With Quote
Old 01-26-2013, 08:58 AM   PM User | #3
rexhvn
New Coder

 
Join Date: Oct 2011
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
rexhvn is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
Do you mean you have EXACTLY the same code for all search elements? You should make sure to change the ID attribute, because each ID attribute can only appear once per document.
The ID's are different for each code....
rexhvn is offline   Reply With Quote
Old 01-26-2013, 09:23 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
When you click on any search item, call a function to loop through to set all of them to style.display="none";, then set the relevant one to style.display="block";

It will make things easier if the divs have incremental ids, div1, div2 etc. But that is not essential if there are only four of them.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 01-26-2013, 12:07 PM   PM User | #5
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!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" xml:lang="en" lang="en">

<head>
  <title></title>
<script type="text/javascript">
/*<![CDATA[*/
function togglebuy(id){
  var div = document.getElementById(id);
  if (togglebuy.div&&togglebuy.div!=div){
   togglebuy.div.style.display ="none";
  }
  div.style.display = ( div.style.display == "block" ) ? "none" : "block";
  togglebuy.div=div;
}

/*]]>*/
</script></head>

<body>

<a onmouseup="togglebuy('buyrevealed');" >Toggle buyrevealed</a>
<a onmouseup="togglebuy('tom');" >Toggle tom</a>
<a onmouseup="togglebuy('joe');" >Toggle joe</a>
  <div id="buyrevealed" style="display: none;">
more code for search funcion 1
</div>
  <div id="tom" style="display: none;">
more code for search funcion 2
</div>
  <div id="joe" style="display: none;">
more code for search funcion 3
</div>
</body>

</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:56 PM.


Advertisement
Log in to turn off these ads.