Concealment vs Deletion of DOM components

Conceal or Erase

When dealing with changing DOM elements, is it better to hide them or delete them? Consider that the environment may change frequently and that elements can have various event callbacks.

Hide:

What is the best approach when hiding elements? If clicking a button should hide multiple items, you can choose to hide them individually or utilize CSS rules for this purpose.

Option 1:

<style>
.superContent{/*...*/}

.superContent.noEdit .occultable{
    display:none;
}
</style>

<form class=”superContent” action=”...”>
    <label>Name</label>
    <input type=”text” />
    <input type=”submit” class=”occultable” value=”send”/>
</form>

<button id=”hideAll”>Edit</button>
<script type=”text/javascript”>
    $(“#hideAll”).click(function(){
        $(“.superContent”).toggleClass(“noEdit”);
    });
</script>

http://jsfiddle.net/p8mU8/

Alternatively, you could simply hide the desired items (which may be a few or many):

Option 2:

<script type=”text/javascript”>
    $(“#hideAll”).click(function(){
        $(“.occultable”).toggle();
    });
</script>

http://jsfiddle.net/JAmF9/


Remove:

To modify the DOM, you also have the option of deleting unwanted items and re-inserting them later.

Option 3:

<form class="superContent">
    <label>Name</label>
    <input type="text" />
    <input id="sendbutton" type="submit" class="occultable" value="send"/>
</form>

<button id="hideAll">Edit</button>​

<script type=”text/javascript”>
$("#hideAll").click(function(){
    if( $(".superContent").find("#sendbutton").length>0 ){
        $(".superContent").find("#sendbutton").remove();
    }
    else{
        $(".superContent").append('<input id="sendbutton" type="submit" class="occultable" value="send"/>');
    }
});​
</script>

http://jsfiddle.net/Yj5Aw/

These are just samples. In a UI with numerous elements, which approach do you consider the most optimal? Which one minimizes memory leaks and maximizes performance?

Some JavaScript frameworks like kendo-ui favor removing elements, while jQueryUI tends to hide them. However, in scenarios like the Tab sortable widget, jQuery temporarily hides tabs as the user drags them.

Answer №1

It seems quite clear, don't you think?

  • Hiding is ideal for re-showing elements.
  • Removing works best when elements are no longer needed.

By hiding and then re-showing elements, their callbacks and data are retained, particularly when using jQuery.

Removing unnecessary elements helps reduce allocated memory for your page, although the impact may not be substantial in most cases.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating a responsive graphic using Bootstrap CSS

I am currently trying to determine how to create individual graphics that can be directly loaded via CSS and made responsive. When I resize the screen, the graphics also adjust accordingly. However, on smaller displays such as mobile phones, the graphics ...

Sending AJAX calls with complex nested objects to a Spring MVC controller

I have a Spring MVC web application and I am looking to incorporate Hibernate and AJAX functionality. In this scenario, I have two entities: Item and CatalogItem. The CatalogItem entity includes various fields and a reference to Item. My question is wheth ...

Ways to eliminate the gap between columns

I need to adjust the spacing between columns on my bootstrap, 3 column web page. Is there a way to do this without directly editing the bootstrap.css file? Thank you. <div class="container"> <div class="row"> <div clas ...

When querying the model, the result may be undefined

I'm encountering an issue where I can't access the content of an array of documents in my model and it's returning undefined. Here is the model structure (Project.js): var mongoose = require('moongoose'); var Schema = mongo ...

Resolving CORS problem: Eliminating the 'Access-Control-Allow-Origin' Response Header in Angular

Recently, the backend API located at has been proxied by an F5 device which automatically includes the CORS header Access-Control-Allow-Origin: * in all responses. However, the GUI code seems to also be adding a CORS header with Access-Control-Allow-Origi ...

Ways to set up information without altering the original

Is there a way to set Vue data "settings" to be equal to another object "original_settings" without altering the original object when "settings" is modified? How can this be achieved? new Vue({ el: "#app", data: { settings: original_sett ...

Conceal an element using a transition effect, initiating its positioning at the center instead of the default

One of the challenges I'm facing is with a video element that I want to hide gradually when clicking on another element. The issue is that after the animation completes, the video ends up hidden at the top-left corner of the screen instead of at the c ...

Center-align text points on a webpage using a bootstrap theme

Using the bootstrap creative theme, I am attempting to create a list of points with the md-bootstrap check fa icon. Here is how it currently appears: https://i.sstatic.net/7cgbG.jpg My goal is to center the text on the page while aligning the start of e ...

Improve Email Regular Expression to Restrict Consecutive Periods

I'm not very good with regular expressions, so I decided to seek some help. Here's the regular expression I have: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.) ...

Trouble with $http response not appearing in AngularJS application

Currently, I am in the process of developing an angularjs application and encountering a challenging issue with setting up the $http connection to a php file. The header is displaying a response that I echoed from php. Nevertheless, two key problems persis ...

Event handler for jQuery AJAX signals

Let's say I have an object called myApi which has a function named execute var api = new myApi(); api.execute(); Within the execute method, I have (*that referring to the myApi instance) function execute() { $.ajax({ type: t ...

Unable to invoke Angular function from within jQuery

I am currently using the angular full calendar plugin and have successfully added a context menu in the event render function. The context menu is functioning properly, but I am facing an issue where I want to call an Angular function when a context menu i ...

How come a Google Maps API component functions properly even without using *NgIf, but fails to work when excluded in Angular 9?

I recently followed the guide provided in this discussion with success. The method outlined worked perfectly for loading search boxes using this component: map.component.html <input id= 'box2' *ngIf="boxReady" class="controls" type="text" p ...

Using ng-transclude directive in AngularJS for input values

My goal is to develop a directive that includes the ng-transclude value in the input field's value attribute within an HTML template: The directive I have created: module.directive('editInput', function(){ return { restrict: &a ...

The div element should stretch to occupy the entire height of the screen

I have structured my webpage with three different divs. The left one is defined with col-md-2, the middle one with col-md-8, and the remaining right div with col-md-2. I want the right div to take up 100% of the screen height, even when no controls are pre ...

Is it better to append content in JQuery rather than replacing it with .innerHTML?

Here is a function designed to retrieve older wallposts from a user, 16 at a time, and add each chunk of 16 to the end of the current list in the div called "sw1". The function works well, except when there is a wallpost containing a video or embedded obj ...

No user was located using Mongoose.findOne()

Utilizing fetch() to make a call to a URL looks like this: const context = useContext(AuthContext); const navigate = useNavigate(); const handleSubmit = (event) => { event.preventDefault(); const dat ...

Using PHP code to pass a value to JavaScript

I have both PHP and JavaScript code on my page, and I need to retrieve a value from PHP into my JavaScript code. Below is my JavaScript code: <script type="application/ld+json"> { "@context": "http://schema.org", "@type": "Bread ...

What is the process for extracting data from latitude and longitude in order to generate a marker on Google Maps using a script

I have an HTML input text and want to pass coordinates to create a marker on Google maps. Check out the code here: jsfiddle.net/#&togetherjs=r3M9Kp7ff7 What is the best way to transfer this data from HTML to JavaScript? <label for="latitude">L ...

What is the recommended approach for font color usage within a single block in HTML5?

In HTML5, the font tag is considered deprecated. If I have a paragraph where I want to display certain words in specific colors, using the outdated font tag may not be ideal. What are some modern and clean CSS techniques that can achieve this same effect ...