I’m struggling to find a tutorial for this and wondered if anyone could help?
I’m after something like what appears on this site, the comments box.
The comments box toggles show and hide, with the plus and minus changing.
With the comments title remaining there also.
It appears below i would need my box to appear above the toggle switch.
I’ve found lots of toggle tuts but none that have an image which changes letting you know what state your in and has some annoying animation when all i want is to appear and disappear, like the above.
Does anyone know of any tutorials which might help me?
It can be done, but you shouldn’t do it!
Those who have a browser that is not capable of javascript / have javascript disabled, will never ever see the information!
The better way would be to let the javascript hide the information and then use the javascript to toggle it.
People with javascript disabled will always see the information, but that’s a good thing, because otherwise they would never see it.
In other words, add $(“#showHideButton”).hide(); as the first line in the $(document).ready() function.
And yes, you would then have to change
is(“:hidden”) to is(“:visible”)
show() to hide()
hide() to show().
P.S. this technique of initially showing everything and then manipulating the the DOM to give users with javascript enabled a different experience is called progressive enhancement, might want to google it
What the code now states is “if the block is hidden, hide it, otherwise (if it’s not hidden, viz visible) show it”. Now that doesn’t do anything, does it?
Or just before the </body>, or create an external .js file with the script above in it, and include that in the page.
This script indeed assumes there is an element with id commentBox that needs to be shown/hidden and an element with id showHideButton which is the button that shows/hides commentBox.