Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
how do i toggle visibility with jquery?
I’m trying to toggle the visibility of 2 divs (same class, different ids) using jquery but I’m hitting walls left right and centre.
the poroblem is that I do not want the elements to use display:none as I prefer visibility:hidden. This is because the later should not affect the layout when the visibility is toggled whereas the first does.
Anyone knows of a js script (preferably jquery) which can do this?
solution here
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
Really don’t understand why display:none doesn’t fit you, but you can manually change requered css porperty:
$(".mydiv").css("visibility", "hidden");
Or, if you need smooth hiding, you can rewrite css property after animation:
$(".mydiv").hide(500)
.css("visibility", "hidden")
.css("display", "");
Or use more short code:
$(".mydiv").hide(500).css({visibility: "hidden", display: ""});
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
Re: how do i toggle visibility with jquery?
Hi victor,
Thanks for the prompt reply. You code hides but it does not toggle the visibility of the div.
the reason I prefer hidden to none can be best demonstrated here which is a js I eventually found but have not managed to make it work on both divs yet.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
getting there… This is what I currently have in the head
<script type="text/javascript">
$(document).ready(function(){
$(".togglediv").toggle(
function () {
$(this).css({"visibility":"hidden"});
},
function () {
$(this).css({"visibility":"visible"});
},
function () {
$(this).css({"visibility":""});
}
);
});
</script>
When I click on the divs they hide but I cannot get them to show again.
Is there a way to have a show/hide link which picks up from the js and toggles the visibilities?
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
Another problem with the above code.
The toggledivs contain links which are now unclickable as the divs hide before the link action occurs.
Any ideas?
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
You need separate buttons or links to which you attach a click.
See this show/hide example on the jquery site (the buttons can be links).
An alternative to directly changing the css is to add / remove / toggle a class which you then give the css you want:
$("#target").addClass("newclass");
$("#target").removeClass("newclass");
$("#target").toggleClass("newclass");
This thread might also help you if you want a toggle button to change text from show to hide and back.
TXP Builders – finely-crafted code, design and txp
Offline
Re: how do i toggle visibility with jquery?
Hi jakob,
Still can’t get it. Changing the text to show/hide would be great but I am still struggling with the basic functionality. Thought that by allocating a button would do it.
This is what I have
<script type="text/javascript">
$(document).ready(function(){
$(".togglebutton").click(function(){
$(".togglediv").toggle(
function () {
$(this).css({"visibility":"hidden"});
},
function () {
$(this).css({"visibility":"visible"});
},
function () {
$(this).css({"visibility":""});
}
);
});
});
</script>
and I try to activate it with a link
<a href="#" class="togglebutton">Toggle</a>
nada:(
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
Almost there!!! I mean it this time:)
<script type="text/javascript">
$(document).ready(function() {
$(".togglebutton").toggle(function() {
$(this).html("Show");
$(".togglediv").css({"visibility":"visible"});
}, function() {
$(this).html("Hide");
$(".togglediv").css({"visibility":"hidden"});
});
});
</script>
and I call it with <a href="#" class="togglebutton">Hide</a>
Only problem is that when the page loads the Hide lnk shows OK. I click it once and nothing happens except that the link text changes to Show.
On clicking again it hides the divs and the link text changes to Hide.
Basically the Show/Hide are reversed due to the need of a double click in the beginning
Can someone point to me where I’m screwing up?
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
Solved:)
A bug? maybe, but I turned it into a feature:)
For those who might want to use it here’s the final code
<script type="text/javascript">
$(document).ready(function() {
$(".togglebutton").toggle(function() {
$(this).html("Hide");
$(".togglediv").css({"visibility":"visible"});
}, function() {
$(this).html("Show");
$(".togglediv").css({"visibility":"hidden"});
});
});
</script>
The difference from the script on my previous post is that I swapped the Hide/Show texts:)
I wish I knew more about javascript as the little snippet above took the best part of my day today.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: how do i toggle visibility with jquery?
A few days later and the solution is finally here:)
In the <head>
<script type="text/javascript">
$(document).ready(function() {
$('.togglebutton').click(function() {
$('.togglediv').toggleClass('hidden');
$('.togglebutton').toggleClass('nodisplay');
});
});
</script>
In the <body>
<a href="#" class="togglebutton">Hide</a><a href="#" class="togglebutton nodisplay" style="display:none;">Show</a>
you can of course move the inline style in your css.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline