Hiding and showing content is MUCH easier with the magic of jQuery. Here is a simple demo of the jQuery .toggle()
function.
Here is the JavaScript needed to run this demo:
1 2 3 4 5 6 | <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript"> function toggleDiv(divId) { $("#"+divId).toggle(); } </script> |
Line 1 simply loads the minimized jQuery v1.4.4 library. Once that is done, the rest is cake =) Line 3 defines the toggleDiv
JavaScript function which takes one parameter: the id of the div to be toggled. Line 4 is where all the action takes place. The $("#"+divId)
part is the powerful jQuery selector which selects the div that was passed to the function. I cannot say how easy jQuery makes it to select elements whether it is by id, class, or even by element types. Once our div is selected, we simply append jQuery’s toggle()
function which automatically detects whether or not the content hidden and toggles it. On the HTML code level, the function toggles the display
attribute between none
and block
.
Here is the raw HTML code for the demo:
1 2 3 4 | <a href="javascript:toggleDiv('myContent');" style="background-color: #ccc; padding: 5px 10px;">Toggle Button</a> <div id="myContent" style="background-color: #aaa; padding: 5px 10px;"> The content in this div will hide and show (toggle) when the toggle is pressed. </div> |
The a tag
is styled to look like a button and the onclick
event uses our toggleDiv
which passes the ‘myContent’ div ID as the argument. The rest of the HTML basically codes for our ‘myContent’ div whose content will be toggled by a click of the a tag
.
That is it! There is no more need to reinvent the wheel my friends.
Here is a new demo in response to a request where only one div is displayed at any one time.
Here is the HTML code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <table> <tr> <td> <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;"> <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a> </div> <div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #1</div> </td> <td> <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;"> <a id="myHeader2" href="javascript:showonlyone('newboxes2');" >show this one only</a> </div> <div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #2</div> </td> <td> <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;"> <a id="myHeader3" href="javascript:showonlyone('newboxes3');" >show this one only</a> </div> <div class="newboxes" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #3</div> </td> </tr> </table> |
The HTML code contains 3 divs to start off with: 2 hidden and 1 displayed. Each link will launch the showonlyone JavaScript function and pass over the corresponding div ID.
1 2 3 4 5 6 7 8 9 10 | function showonlyone(thechosenone) { $('.newboxes').each(function(index) { if ($(this).attr("id") == thechosenone) { $(this).show(200); } else { $(this).hide(600); } }); } |
Line 2 contains some very cool uses of jQuery. Here we are looping through all divs with the class = "newboxes"
which represents all 3 divs.
Line 3 checks to see if the div id in the current loop is equal to thechosenone which is the argument that was passed to the function.
If the id matches up, we use the .show()
jQuery function to display the div. The argument 200 used in the .show()
function will animate the display for a duration of 200 milliseconds. How cool is that =) If you leave it blank, the div will appear suddenly.
If the id does not match up, we simply use the .hide()
jQuery function to hide the div. The argument 600 used in the .hide()
function will animate the div so that it looks like it is sliding away. I used a higher number here so that you can see the difference in speed of the animations. Again, you can simply leave it blank if you want to make the div suddenly disappear.
That’s it!
If you would like all the divs to be hidden from the get go, simply use CSS to hide them all like so:
.newboxes { display: none; } |
Here is another quick example using the jQuery .slideDown()
and .slideUp()
functions.
Everything is the same here except we are calling the .slideDown()
and .slideUp()
functions instead of the .show()
and .hide()
functions respectively. Even the animation duration is kept the same if you pass an argument to the function calls.
Here is the JavaScript code:
1 2 3 4 5 6 7 8 9 10 | function slideonlyone(thechosenone) { $('.newboxes2').each(function(index) { if ($(this).attr("id") == thechosenone) { $(this).slideDown(200); } else { $(this).slideUp(600); } }); } |
Not much explanation needed here. Same idea, different functions used.
Here is the HTML code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <table> <tr> <td> <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;"> <a id="myHeader1" href="javascript:slideonlyone('newboxes1');" >slide this one only</a> </div> <div class="newboxes2" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;">Div #1</div> </td> <td> <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;"> <a id="myHeader2" href="javascript:slideonlyone('newboxes2');" >slide this one only</a> </div> <div class="newboxes2" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #2</div> </td> <td> <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;"> <a id="myHeader3" href="javascript:slideonlyone('newboxes3');" >slide this one only</a> </div> <div class="newboxes2" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #3</div> </td> </tr> </table> |
Same thing here except we are calling the new slideonlyone()
function that we just defined.
If you want all your div content to be hidden from the get go, simple use CSS to hide them like so:
.newboxes { display: none; } |
Here is the HTML code:
1 2 3 4 5 6 | <a id="aTag" href="javascript:toggleAndChangeText();"> Expanded text mode ▼ </a> <div id="divToToggle"> Content that will be shown or hidden.<strong> </div> |
Here we have an a tag that has the text that we want to toggle as well as the div content. Whenever the link text is clicked, it fires off the toggleAndChangeText() JavaScript function.
Here is the JavaScript code for the toggleAndChangeText() function:
1 2 3 4 5 6 7 8 9 | function toggleAndChangeText() { $('#divToToggle').toggle(); if ($('#divToToggle').css('display') == 'none') { $('#aTag').html('Collapsed text mode ►'); } else { $('#aTag').html('Expanded text mode ▼'); } } |
Whenever the toggleAndChangeText() is fired off, it will first toggle the div with our nice jQuery toggle() function in line 2. Then it will test if the div content is hidden (or if the display attribute is set to none) in line 3. If so, it will change the link to “Collapsed text mode ►″. Otherwise, the link text will be changed to “Expanded text mode ▼″ in line 7.
To reverse the effect and start with the content hidden, all we have to do is the following:
1 2 3 4 5 6 | <a id="aTag" href="javascript:toggleAndChangeText();"> Collapsed text mode ► </a> <div id="divToToggle" style="display: none;"> Content that will be shown or hidden.<strong> </div> |
The inline CSS is the key to making this work. We start with the content hidden which our JavaScript will detect so it will toggle to make the content visible once the link is clicked.
Next up is a pretty cool demo where we use the .animate()
jQuery function to help toggle just about any element. In this case, we’ll use an image of yours truly :)
Here is the HTML code:
<div id="clickme" style="background-color: #333333; color: #FFFFFF; padding: 10px; width: 200px; cursor:pointer;"> Click here to toggle me in and out =) </div> <img id="me" src="http://www.randomsnippets.com/wp-content/uploads/2011/04/2.png" alt="It's me....ah!!!" title="Allen Liu" width="100" height="77" class="alignnone size-full wp-image-552" style="border: none;" /> |
I have to apologize for all the inline CSS. This is the easiest way for me to get these demos across without having to separate the styling (which is usually best practice). I am trying to do good here so I hope the HTML Gods
will spare me this one time.
Anyway, the HTML is quite simple. It is just a div with some text inside and a silly image. Notice there is no JavaScript event attached to our div just yet. This is where our jQuery code comes in and saves the day!
1 2 3 4 5 6 7 8 | $(document).ready(function() { $('#clickme').click(function() { $('#me').animate({ height: 'toggle' }, 2000 ); }); }); |
In line 1, we tell jQuery to wait until the DOM (Document Object Model) is fully loaded or when our page is fully rendered. We do this because we want to add functionality to our “click me” div. If the “click me” div has not yet loaded and this JavaScript executes, there is no element to work on. Game over.
Instead, we tell jQuery to wait until our “click me” div is in place, and then add a click event to it (line 2).
Lines 3-6 defines our click event by animating the $('#me')
element which in this case is the image. If you are not used to jQuery syntax, $('#me')
uses jQuery’s all-powerful selector to help us select the element with id = me
.
Once we have our element selected, we apply the animate
function to add animation to this element’s CSS properties. We first begin with line 4 which toggles the height of the element and preserves the height:width ratio which gives it a shrinking effect. We use toggle here so that you can keep pressing the button to toggle in and out. You can of course use show
or hide
just like in the previous demos.
Line 5 just indicates how long the duration of our animation will last in milliseconds. In this demo, it is 2000 milliseconds which translates to 2 seconds (I was in AP math in high school).
Here is the HTML code along with the CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <style type="text/css"> .evenNumber, .oddNumber { width: 100px; height: 100px; margin: 10px; padding: 10px; float: left; background-color: #333333; color: #FFFFFF; } </style> <button onclick="toggleByClass('oddNumber');">Toggle odd numbers only</button> <button onclick="toggleByClass('evenNumber');">Toggle even numbers only</button> <div class="oddNumber">1</div> <div class="evenNumber">2</div> <div class="oddNumber">3</div> <div class="evenNumber">4</div> <div class="oddNumber">5</div> <div class="evenNumber">6</div> |
Lines 1-11 covers the styling of the divs so they are presentable.
Lines 12-13 codes for 2 buttons that toggles the odd number divs and even number divs respectively by calling the toggleByClass
function and passing the respective class names.
Here is the JavaScript code:
1 2 3 | function toggleByClass(className) { $("."+className).toggle(); } |
Simple and sweet but a lot is going on here. Let’s dissect line 2 and cover each component separately.
$("."+className)
uses the jQuery class selector with the className
variable representing the class of the element(s) you want to select. In our demo, we use the oddNumber
and evenNumber
class names to select for their respective divs.
Once the divs are selected, we tack on the .toggle()
jQuery function to hide and show the elements.
Here is a tutorial that shows how to hide or show content based on links on different pages.
That is it for yet another demo =)
If there any other demos/tutorials that you would like to see on this subject, please feel free to comment below and I will try my best to implement it.
Note: If you have an issue with your code, please give me a URL to work with. It’s extremely difficult for me to help if I cannot actually see the code.
If you found that my code was helpful in any way, shape, or form and would like to buy me a beer, please use the Donate button below =) Cheers!
Hi i love your nice simple to use plugin for doing drop downs.
i have a question in my example i wanted a way to close/calaps the the div with a button or even a timer mouse out event? is this possible?
here is my example, but the close button only works because i haven’t entered a href”" location? im sure you know a better js toggle option.
http://jsfiddle.net/ecomatt/gwK6d/11/
Thanks for you help
Hi Matthew,
I think this is what you’re looking for:
http://jsfiddle.net/gwK6d/18/
Allen
Hi Allen, this is incredibly helpful, I’ll be using parts of these examples in my own site shortly.
I do have a question regarding the last example, how would I say use this in conjunction with the .animate() jQuery function ? So say when you toggle your elements, they disappear and reappear nicely instead of abruptly. Is this possible?
Thanks!
Hello, I like these scripts, they are amazing! One question about toggleAndChangeText function. I have that example and it’s working fine, similar to this http://jsfiddle.net/Mb7hM/20/
. But how to do change it so that you can do multiple divs on one page? Thanks!
To be more specific, look at this:
http://jsfiddle.net/mYm3Q/
All I am asking is to how to “separate” aTag. That’s where I am lost, thanks!
Sorry, I’ve been extremely busy. Providing the jsfiddle makes it much easier for me to help fellow peeps out.
Here you go :)
http://jsfiddle.net/mYm3Q/3/
wonderful! Thank you so much! It works like a charm! I appreciate it.
You’re very welcome :)
Thank you! Perfect! One more question….. I’m also wanting to combine this solution with your .slideDown() and .slideUp() example… displaying only one div at a time (I’ll have several).
Here is the same function but using the .slideDown() and .slideUp() jQuery methods.
http://jsfiddle.net/axl163/8w6GG/5/
Allen
Hi Allen. First, many thanks for such a great tutorial! I’m using toggleAndChangeText() and am wanting to have the cell load as collapsed. With click, it reveals/opens the hidden cell. What changes are needed? Thanks!
Hi Jennifer,
I have updated the post to include the solution :)
I hope it helps.
Allen
Hi Jennifer,
Here is a JSFiddle that toggles all items to the same state. I used the jQuery library which tidies up the code a bit. I hope this helps.
http://jsfiddle.net/axl163/8w6GG/4/
Allen
Allen,
Big thx for u for these examples
i just need some help at the animated example which have the Guy who says click here to toggle, etc….
i need to make it hidden and when i click on it start to appear, i know it’s easy but i can’t do it :(, so please i need some help.
Sorry Allen, i got it
just make
#me{display:none}
as always as you say, Thank you again.
Allen,
I’m trying to take the links out of the table and put them in a div, but the underline function seems to stop working. I would also like div #1 to be displaying by default until another link is clicked.
Hope you can see what I’m trying to do here:
http://jsfiddle.net/pQDAc/32/
Thanks for the help with this.
You had some CSS that was overriding the
underline
class. Here you go:http://jsfiddle.net/axl163/pQDAc/33/
Hi the first code is not working in my machine please help me ….
Hi Naveen,
In order for me to help you, I will need details as to the type of error(s) you are seeing. If you have a url to the actual page, that would be best. If not, you could also create a jsfiddle for me to look at.
Allen
Hi Allen,
I have noticed that in IE and Firefox the links stay underlined once they are selected, however in Chrome this doesn’t seem to be the case.
Do you know of a possible fix for this?
Beer in it for you if you can let me know!
Thanks.
Matt
Hi Matt,
Can you point me to a URL where you are experiencing this issue?
Allen
Hi Allen,
Thanks for the reply.
It’s doing it with the examples on this page & on my page but I am working locally (can upload if need be)
But if you open this page in Chrome and click any of the links i.e. “slide this one only” the links don’t stay underlined when they are selected.
They do in IE & Firefox but I need them to stay selected in Chrome so the user knows which content area they are viewing.
Hope that makes sense!
Hi Matt,
I see the issue now. I have created a jsfiddle for you which should address it:
http://jsfiddle.net/axl163/pQDAc/6/
Now, I simple add a class to the link that is clicked and you can style that class however you like :) Currently, it is simply an underline.
I hope this helps.
Allen
That’s perfect thanks Allen, enjoy the beer, cheers!
Matt
I’m glad it worked out for you. Thanks! :)
Hi Allen,
Sorry if you have answered this before but I just wanted to check how you are making the links underline when they are selected.
I don’t seem to be able to replicate this. When the links are clicked is there a certain class that is applied to the elements so that they are underlined until another link is clicked?
Any help would be much appreciated.
Thanks.
Matt
Don’t worry I have sorted it!
I had a stylesheet overriding the styles in your example!
Thanks.
Matt
Hello Allen
I’ve been trying to get your example to work on my website, but it just doesn’t. Works in jsfiddle fine, but doesn’t in the actual site. Please help. Thanks. Heres the link http://www.dwphoto.us/sad.html
Hi Daniel,
This one was an odd one. There is an illegal character on line 19 which is just the empty line before your closing </script> tag. I removed the line and everything worked.
Allen
hi, allen
this is super! i am having some trouble trying to do something quite simple; here is my hidden page:
http://wallcentrekerrisdale.com/indexShannon.html
the ‘HIDE / SHOW’ button under the div called ‘content’ works, and it also hides the div called ‘footer’, but all i want to do is change the ‘HIDE / SHOW’ text to ‘SHOW’ when closed and ‘HIDE’ when both are open
your tutorials are fab but if you look at my source code i can’t quite seem to get it
and i will buy you a CASE of beer, promise :)
thanks, jodi
Hi Jodi,
Here is a jsfiddle of the code that should do what you are requesting:
http://jsfiddle.net/axl163/uFv2V/9/
Hope it helps.
Allen
Yes, indeed! I am going to buy you a case of beer right now; thanks so much, Allen :)
I’m glad it worked out! =) Thanks for the case!!! I really appreciate it.
Allen,
You rule… I have no experience and I can actually get your examples to work on my own. Thanks for the education!
Great to hear Matt! Glad it helped =)
Allen
Hi Allen,
Thank you so much for this wonderful code! I am using the jquery slideup slidedown function and was wonderinf if there was a way to change the image of the button when it is not clicked. I would like to have the non showing button a lighter color. Any help would be much appreciated! Thank you so much!
Website example page: http://www.poochieprints.com/shopstationerybow.php
Hi Stacey,
I’m not sure which button you are referring to on your site, but you can control a button’s style with CSS and using
background-color
. That is probably the simplest way. Here is an example:http://jsfiddle.net/axl163/jQNva/
Allen
Hi Allen.
I found your code extremely useful.
My problem (beside is the first time I use JQuery) is that I’m trying to apply the adapted ‘slideonlyone’ function to 2 different sections of my website (the main menu and some listed contents) but I only get to make one of them work at the time (the listed contents).
How do I have to write the jquery scripts in the head section in order to get both sections to work?
WEBSITE: http://telar.com.mx/bgbg_website/es/servicios.html
*Right now both jquery scripts are just pasted in the template code and only one works but I’ve tested both separately.
I know this may be a basic question but I’m new in this :(
Regards from Mexico!
You’ll certainly get a mexican beer as donation.
Hi erandisotelo,
The reason why it is not working is because you have both functions defined with the same name. To generalize the function, try replacing both with just this one:
To use this function, use the same exact code that you have:
I hope this helps!
Allen
Wow. That worked immediately.
Oh! But I just realized that this function doesn’t close the displayed div when I click a second one. Could you help me?
Allen! I changed the name of the functions in the script and code and now it works as I expected. Is that a correct solution?
Hey Allen! thanks for that code! its been very usefull. Im having troube with my scrollbar once my div shows. Im using the first code , but it seems that there is a problem with the Display:none; property.
Do you have any idea of how can I fix that?
Cheers!!
Hi garzastep,
Can you give me a link to your site? It is hard for me to really understand your issue without an example to work with.
Allen
Hi Allen.
Love what you did here.
I tried to modify it to have sliding toggle, but button doesn’t change the name like with .toggle:
http://jsfiddle.net/bartula/Mb7hM/1/
Should it be working, or Ive missed something here
Thanks
Here you go :)
http://jsfiddle.net/Mb7hM/20/
The callback function is the key to making it work.
Allen
Hello Allen.
Great examples. :)
If you could take a look at my code:
http://jsfiddle.net/kafarek/37aew/
I’m trying to modify it, so when i click on Option1 or 2 or 3 or 4 it shows Option5, 6, 7, 8 but 1,2,3,4 doesn’t dissapear?
Thanks in advance :)
Hi,
Options 1,2,3,4 are disappearing for me. Are you referring to something else?
Allen
Hi there!
Awesome code you have here :) I’ve only started programming this year, and this is the first time I use Jquery. I used the first code in the top of the page, where the toggle is visible from the start, how do I hide it from the start? Can I use CSS like you did with the other codes?
Also, is it possible this doesn’t work in IE8?
Thanks in advance! :)
Hi Simon,
Yes, you can use CSS to hide it from the start. Example:
Allen
Hello again Allen,
The following script works just fine…:
I’m curious though, how could the script be adjusted so as to CLOSE THE ACTIVE DIV AFTER AN 8 SECOND DELAY, if a user doesn’t click another button? Of course, this would need to happen while maintaining the current functionality. Thanks in advance for your help!
Hi Ullysee,
You can use the jQuery .delay() function and chain it to the event like so:
Here is the usage details delays for .delay().
Allen
Hi Allen, thanks a lot for the useful code.
I am using “showonlyone” to toggle my page’s content, and I want all of it to be hidden when the page loads. This all worked fine until I tried to put an image slider (bxSlider) in one of the boxes : it seems like the “display:none” in the CSS is messing with it. Observe : http://megalife.fr/test/test.html
Putting a “display:block” in the slider div doesn’t help.
If I remove the “display:none” from the newboxes CSS, the slider works : http://megalife.fr/test2/test2.html
…but all content is shown. Can you think of any way to fix this? Can I have all the divs initially hidden without using CSS?
Thanks!
Well, I fixed it! I replaced .newboxes {display:none;} with .newboxes {visibility:hidden;} in the CSS. :)
Hi Allen and many thanks for sharing this code.
How do I preserve the state of an open tab used in demo #2? I want to use the tabs in a checkout form and when there is a missing input field I want to show the chosen tab again.
Thanks so far
Chefkoch
Hi Chefkoch,
To preserve state, you can save the information to a cookie. That is the quickest thing I can think of that would be easy to implement. Of course, this would not work if someone has it turned off.
Allen
I get it… I need a nap:
http://jsfiddle.net/OQpro/h5eTU/1/
Just saw your jsFiddle after my reply =)
Here you go:
http://jsfiddle.net/axl163/h5eTU/2/
My apologies Allen,
What I need is for the link to fade away, just after a user clicks it.
Ah…here you go: http://jsfiddle.net/axl163/h5eTU/5/
I’m feeling like such a rookie right now o_O. How do I prevent the code from becoming active within the post?
Thanks!
No worries. To comment out the code, use the following tags:
I had to place a space in the closing pre tag but you get what I mean =)
Excellent! Here goes…
You can fadeout with the following example:
http://jsfiddle.net/axl163/M3skq/1/
I recommend using jsFiddle from now on to show your code. It’s much more efficient this way =)
Allen
Good day Allen!
I’m curious to know if .fadeOut can be implemented within the in-line jQuery you provided:
I’d like the element to fade out (at an adjustable speed) just as soon as the link is clicked. Once again, any suggestions you can offer will be appreciated.
My apologies Allen. I posted the resolved string by mistake. Here’s the code, minus the closing :
Re: DEMO 2 “where only one div is displayed at any one time.
”
Hi Allen,
I’ve been search hi and low for a “hide/show” solution THAT WORKS exactly the way I needed it to from the jump. You’ve hit it out the park!!
Would it be possible to include a “toggle” text link at a seperate location where users can click to close the active div? My goal is to implement this script using thubnails in the place of “show this one only” text. In fact, if users could close the active div by clicking the icon a 2nd time, that would be excellent for what I had in mind.
I respect your time and appreciate your contributions, and I realise “kudos” don’t cover hosting fees. I look forward to making it count by putting that yellow button to proper use! =)
Thanks again!
Hi Ullysee,
Yes, that is certainly possible =) You can have the following:
This will close all divs with class = newboxes. If you really want to toggle the divs, you can replace hide() with toggle() instead.
I hope this helps and thanks for clearing the dust off of that yellow button =)
Allen
Allen,
Thanks for the rapid reply! I just tried the suggestion you provided, however, when the link is clicked all I see is White Screen w/ “[object Object]” in the upper right corner of the browser. I was sure to use your snippet exactly as provided.
Any suggestions?
Hi Ullysee,
Can you post what you gave to jsFiddle? This will allow me to edit/debug your code directly. I’ll be online for another couple of hours. If you reply with a link to your jsFiddle, I’ll debug it right away.
Allen
Just finished setting it up…
http://jsfiddle.net/OQpro/UN6C6/4/
Thanks for your help!
Here you go =)
http://jsfiddle.net/UN6C6/5/
It was my bad. I needed to put “return false” in there to prevent the default behavior of the a tag.
Allen
Or you can give this version a go:
If this does not work, let’s go the jsFiddle route.
Allen,
Works like a charm… You Da’ Man!!!!
I’m glad it worked out for you =)
Allen
Hi Allen,
Thanks for these tips! I’m still very new to Javascript/JQuery, so I was wondering if there was a way to combine the “show this div only” code with the “toggle link text” code without writing a new if/else clause for each link/div pair? Would very much appreciate the help, if it’s possible (and will gladly donate for your trouble)!
Thanks much!
Hi Nicholas,
The sample code uses the jQuery
$.each()
function that iterates through selected elements so there should be no need to write new if/else clauses. I may be misunderstanding your request though. Can you post some sample HTML and JavaScript of what you have so that I can simplify it for you?Allen
Ok, here’s a basic example of what I’m working with, with just two / pairs instead of the several I hope to use on my site.
http://jsfiddle.net/nZLpk/
The fade in/out (swapped for show/hide from your snippet) works beautifully, but my added code for applying CSS changes to the text isn’t I’m trying aren’t…and ideally, I’d like to avoid writing if/else scripts for each id and boil it down to a selector function. What do you think? I appreciate it!
(oops, some of that text hyperlinked accidentally, here’s how the first paragraph should read:” Ok, here’s a basic example of what I’m working with, with just two link-div pairs instead of the several I hope to use on my site”)
Hi Nick,
I hope this is what you are trying to accomplish =)
http://jsfiddle.net/nZLpk/8/
This is a bit more elegant and gets rid of the if/else statements by binding all the links on the click event =)
Allen
You are a scholar and a gentleman! Have a beer on me tonight! :)
Many thanks,
Nick
No problem =)
Allen