JQuery slideToggle display type
Asked Answered
F

5

8

I'm using a jQuery slideToggle to show a hidden table row but it sets the display style to block and I need to to display table-row. any ideas how i could accomplish this?

thanks in advance

Fourscore answered 12/8, 2009 at 13:37 Comment(0)
H
5

I figured out a solution to this problem - check if the display has been set to block (if the element has been toggled to be shown) and if so set to desired display type.

$('a.btn').on('click', function() {
    $('div.myDiv').slideToggle(200, function() {
        if ($(this).css('display') == 'block') $(this).css('display', 'table-row'); // enter desired display type
    });
});
Howse answered 28/2, 2013 at 17:11 Comment(4)
note the question, he asked how to set the inline style to table-row , not inline-block -1Engler
@Engler Note my code- the commented section that says "enter desired display type". That's pretty weak to downvote the only working solution to this question.Howse
was weak to download mine which satisfied the question...anyway upvoted youEngler
@Engler Thanks, I appreciate it. The reason I downvoted yours was because I actually tried it. The problem with your answer is that slidetoggle() shows AND hides, but with your modification, it can't hide.Howse
O
4

inorganik's solution is almost working. But you also need to set the step callback to change block to table-row.

        $('row-selector-here').slideToggle({
            duration: 'fast',
            step: function() {
                if ($(this).css('display') == 'block') {
                    $(this).css('display', 'table-row');
                }
            },
            complete: function() {
                if ($(this).css('display') == 'block') {
                    $(this).css('display', 'table-row');
                }
            }
        });
Ossifrage answered 14/1, 2014 at 0:41 Comment(1)
This also works for when setting display to flex rather than table-row. I also had to add another if clause to detect when it sets it to none, and sets it to '' so external css could apply without being overridden (jquery messes with the element's css directly in the html, which overrides css anywhere, unless !important is used, which I didn't want to use).Ernestineernesto
E
2

You can use the callback to change this

$('#tableRowId').slideToggle( function(){
   $(this).css('display', 'table-row');
});
Engler answered 12/8, 2009 at 13:44 Comment(2)
The issue with this is that it sets the display style to table-row every time, when half the time you really want it to be hidden.Rodrickrodrigez
Same problem, had to downvote it. slideToggle() shows and hides, this makes it so it can't hide. Check my answer belowHowse
S
0

This is a known bug (also here). with jQuery slideToggle. There isn't any way to use slideToggle and have it properly preserve the display style. You can apply the display style with a callback when the animation completes, but that may not achieve the effect you desire.

Sofar answered 12/8, 2009 at 13:47 Comment(1)
It's worth noting that the bug report says using simply .toggle() works as expected.Araujo
N
0

just add this code to callback function

$(this).css('display','');

Necessitate answered 23/1, 2010 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.