Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.removeClass('visible').addClass('invisible'); vs. .hide() or .show()

I am an intern and I came across some code like the above in the title.

$(".someClass").removeClass('visible').addClass('invisible');
$(".someClass").removeClass('invisible').addClass('visible');
//there are css classes for this that set display to none etc.

It is buggy and I was charged with changing some UI stuff so I thought I would replace the above with .hide() and .show() correspondingly. The bugs have more or less disappeared. What are the advantages of one way over the other? In this case the latter appears to have worked better, so why would you implement it the first way?

EDIT:

I am just trying to show and hide page elements depending on what page options have been chosen. Which option is best practice?

like image 665
ford prefect Avatar asked Jun 11 '14 15:06

ford prefect


People also ask

What is addClass and removeClass in jquery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

Which method is used to hide Show elements and to Show hidden elements?

The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

How do you remove a class in CSS?

To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method.

Which menu is used to remove the class?

removeClass() method removes the specified classes to the matched elements while animating all style changes.


4 Answers

 $(selector).hide();

and

 $(selector).show();

simply adds

 style="display:none;"

and

 style="display:block;"

respectively to the selector.

When you add and remove classes, you can add a myriad of styles and manage them in your style sheet.

Personally, when I'm simply showing or hiding a selector via jQuery, I use

 show();
 hide();

or

 toggle();
like image 96
Stephan Robberts Avatar answered Sep 20 '22 08:09

Stephan Robberts


.removeClass('visible').addClass('invisible'); - gives an element a class, that can have one or more properties, in this case it's probably display: none and display: block (or table, or.. something else, you can't know)

.hide() and .show() - does not mess with the element's classes, it changes its style directly with display: none and display: block (or whatever type of visibility it had before)

It depends on the situation which to use. You have more control when you add and remove classes though, but probably less performance.

EDIT: Holy Moly, the comment dude is right, I have been living in a lie. I wonder if I experienced a bug or something back in the day when I was 100% sure it did not restore its default visibility, but assign "block" to it by force.

jquery site says:

This is roughly equivalent to calling .css( "display", "block"), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

like image 33
Andrei Cristian Prodan Avatar answered Sep 19 '22 08:09

Andrei Cristian Prodan


Hide and show methods are using style inline code

style="display:block"
style="display:none"

And show method always add "display:block". If you need use for example display:inline-block - you should use your classes, if not - you can use show and hide.

Also instead of combination show and hide you can use method toggle

like image 39
yAnTar Avatar answered Sep 22 '22 08:09

yAnTar


Generally hiding and showing elements via CSS is a good practice, but this is a bad way of going about it.

If you're going to hide and show elements by toggling CSS classes, do so by toggling a semantic class on the highest level element that makes sense. Move a single higher-level element between known states like .showing-post or .creating-comment, and let that trickle down to all its children elements, hiding some elements, resizing others, etc etc.

Don't add classes like invisible or visible to the elements themselves, that's drastically over-complicated and a reinvention of the already available .show() and .hide().

like image 24
meagar Avatar answered Sep 20 '22 08:09

meagar