Ways to conceal a dynamically generated div upon page load?

I am currently facing a scenario where I need to dynamically create a div. My initial approach was to create it on the document ready event, but the requirement is for it to be displayed only upon selection. The problem I am encountering is that the empty div gets created during page load. Therefore, my goal now is to hide the div initially and show it only when text is selected.

JavaScript

$(document).ready(function () {
closePopUp();
var replaceDiv = document.createElement('div');
replaceDiv.id = 'rplceTxtDiv';
document.getElementsByTagName('body')[0].appendChild(replaceDiv);
var innerspan = document.createElement('span');
replaceDiv.appendChild(innerspan);
innerspan.innerHTML += '˟';
var innerDiv = document.createElement('div');
replaceDiv.appendChild(innerDiv);
innerspan.addEventListener("click", closePopUp, false);
replaceDiv.addEventListener("click", getSel, false);
var rplceTxtDiv = $('#rplceTxtDiv');
$('#mytextarea').on('select', function (e) {
    var txtarea = document.getElementById("mytextarea");
    var start = txtarea.selectionStart;
    var finish = txtarea.selectionEnd;
    rplceTxtDiv.offset(getCursorXY(txtarea, start, 20)).show();
    rplceTxtDiv.find('div').text('replace with stars');
}).on('input', function () {
    if (interval) {
        interval = false;
        edits.push($(this).val());
        if (edits.length > maxHistorySize) edits.shift();
        setTimeout(() => interval = true, saveInterval);
    }
});
document.onkeydown = undo;
});

Check out the code on plunker

The visual representation of the issue can be seen in the following image:

https://i.sstatic.net/BsFYj.png

Answer №1

Conceal the DIV prior to adding it to the webpage using

replaceDiv.style.display = "none";

I suggest revealing it again after making changes to the content by

replaceDiv.style.display = "block";

Answer №2

To assign a CSS class, follow these steps:

.ExampleClass {
  visibility: hidden;
}

Then, using JavaScript:

document.getElementById('myElementID').classList.add('ExampleClass');

Answer №3

Simply use the hide() method to hide all div elements with the class name "yourclass". This can be done like so:

For example, you can include this in your document ready function:

$('div.yourclass').css("display","none");

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

Enhance the CSS Transition for a Seamless Bootstrap Toggle Menu Experience

I attempted to modify the Bootstrap Toggle Menu slide effect from top to bottom to left to right. You can view the website here: After experimenting with various CSS codes (I prefer using pure CSS for this effect), I finally discovered this one: .collaps ...

Adjusting the parent with CSS transitions to perfectly align with the final height of a

Jade example div.parent div.child1 div.child2 Upon initial load, Child1 is visible. Clicking will hide Child1 and make Child2 visible. Another click will reverse this action. During the transition between hiding and showing the children, there s ...

The event.pageY position consistently reflects the laptop screen size rather than the exact position where the click occurred

My webpage is scrollable, but the event.pageY position always corresponds to my screen size. Even when scrolling down and clicking near the top of the screen, it registers as 50px. I am currently utilizing event.pageY While this functions correctly on a ...

What is the best way to address caching fonts and files within CSS?

Utilizing the font icon for displaying icons may result in difficulty when adding new icons to the font due to caching issues. Clearing the cache is often necessary to refresh the display. How can I address this concern? ...

"The Vue.js/JavaScript object updates, but the changes are not being displayed in the document object model (DOM

Currently, I am endeavoring to iterate through an array of objects and display them as list items. There is a click listener that introduces a custom property to the object, and a class binding depends on the value of that property. Would you mind reviewin ...

Struggling with jQuery fadeIn and fadeOut in IE8?

I am struggling to understand why my fadeIn, fadeOut, and corner() functions are not working in IE8 on my website at . They were functioning properly before, but now they seem to be broken. Can anyone pinpoint the issue causing this problem? If you click ...

Transform a string into a property of a JSON object

One of my challenges involves extracting a value from a JSON object called Task, which contains various properties. Along with this, I have a string assigned to var customId = Custom_x005f_f3e0e66125c74ee785e8ec6965446416". My goal is to retrieve the value ...

Using Vue and vue-multiselect to create interactive options based on the selected language

I'm currently developing a Vue website with multilingual support. The chosen language is stored in a Vuex store and accessed through the computed property lang, like so: lang(){ return this.$store.state.lang } I use this lang property in v-if cond ...

The hyperlink and criteria provided are accurate, however, the webpage remains stagnant

My login page is supposed to redirect to the contact confirmation page when I receive a 200 response from the server, but it's not working. Can you help me find my mistake? const [form, setForm] = useState({}); async function checkLogin() { tr ...

Issues with Bootstrap Carousel Slide Show functionality in FireFox

I have implemented a Bootstrap-5 Carousel Slide Show for the website banner. It is functioning correctly in Google Chrome, but there seems to be an issue when viewed in Firefox. The Firefox browser version I am using is 90.0.2 (64-bit). Upon inspecting the ...

Choose an Option that Does Not Allow PHP as a Value

Currently, I am retrieving a value from the database where interval days are set to 5. However, when I display it, it always shows '0' within my option form. <?php $interval_day = $this->crud_model->auto_order_interval_day(); ?> ...

Is it possible for Express in Node.js to handle multiple static folders at once?

Currently, I am working on a project that involves a user uploaded collection of styles, scripts, and images as well as my app's own collection of these resources. They are stored in separate directories on my server. I am wondering if there is a way ...

How come require() doesn't resolve the image path when passed as a prop in NuxtJS?

I am encountering an issue in my NuxtJS project where a component is not displaying an image correctly. Despite passing the image path directly to :src="imageAddress", it does not resolve nor throw an error. I attempted using the path inside requ ...

Tips for automatically setting focus to the following cell after inserting a new row in AngularJS

Transitioning from the Knockout world to Angular, I am facing an issue with a key-press event for tab. Whenever I add a new row in the table, the focus shifts to the information icon in the URI bar instead of the next cell in the newly created row. I belie ...

JavaScript encountered an issue while parsing XML: the format is not well-formed

I keep seeing an error message saying "Error parsing XML: not well-formed" when I encounter this line in my javascript code: for (var i=1; i<=totalImgs; i++) If I remove the < character from the line, the parsing error goes away. However, the javas ...

How to convert typescript path aliases into relative paths for NPM deployment?

I am currently working on a typescript project that utilizes paths for imports. For instance: "paths": { "@example/*": ["./src/*"], } This allows the project to import files directly using statements like: import { foo } from "@example/boo/foo"; Whe ...

Obtain an array containing only the specific values of each object

Currently, I have the following code snippet: <db.collection>.find({ id : { $exists: true } }) This query retrieves all documents containing an id property. However, I am interested in transforming this result into an array that contains only the va ...

Confirm if the username is present in jQuery PHP

I am currently working on a functionality to verify if a username is already registered in my application using jQuery, Ajax, and POST method. HTML <div class="form-group"> <label for="username" class="col-md-3 control-label">Username< ...

CodeIgniter's feature of a dynamically dependent dropdown list

I am facing an issue with implementing three Dynamic Dependent Dropdown Lists in Codeigniter:- The first dropdown represents company names Based on the company selected, I aim to display that particular company's managers in another dropdown The thir ...

Execute a JQuery script exclusively on child elements belonging to a specific parent element

I am looking for a simple method to execute a JQuery script only on a specific form and its elements without impacting other forms on the site. Currently, I am using the child selector along with other selectors, but is there a way for the subsequent sele ...