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:

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

Tips for including a close button in a slidedown panel

Having this as a functional slidedown effect: ::-webkit-scrollbar { width: 0px; /* remove scrollbar space */ background: transparent; } /* optional: show position indicator in red */ ::-webkit-scrollbar ...

What is the best way to create individual clickable images that open up a modal window for each one?

Currently, I have created a function that is able to map images from an array in Next.js using react-bootstrap. The function is functioning as expected, however, my goal is to add an onClick function to each image so that when clicked, it opens up in a new ...

Can REST calls be initiated when the CSP is unable to be modified from the default-src: 'none'?

Sometimes, I wonder if this question is silly because it goes against the purpose of the Content Security Policy. There's a webpage located at foo.baz.com that requires data from bar.baz.com to function locally. Below is the code snippet for the func ...

Is there a delay in Javascript identifying user existence when retrieving values from ajax?

I'm working on a script that checks if a username already exists before allowing a visitor to proceed. Here's a snippet of the code I'm using: EDIT: I've made some adjustments based on your feedback, but I'm still having trouble g ...

Utilizing jQuery to conceal table rows, streamlining the process

Currently, I am attempting to dynamically show and hide table rows using a toggle link that reveals or conceals them when clicked. While I have successfully implemented this feature, I am exploring ways to create a more generic function instead of duplic ...

Collapsing or expanding the Material UI accordion may lead to flickering on the page

import React from "react"; import { makeStyles } from "@material-ui/core/styles"; import Accordion from "@material-ui/core/Accordion"; import AccordionDetails from "@material-ui/core/AccordionDetails"; import Accordi ...

Is there a way to iterate through objects and add new properties to them?

I am trying to achieve the following object: let newPost = { title: "Post 1", Content: "New content" } with the code below: let newPost = {}; let postData = $(".post-data").each (function(index) { newPost.title = $ ...

The reload/refresh feature for images is not functional

When generating images automatically, I noticed that the file with the same name gets updated but is not reflected in the browser. $("#image_test").attr("src", data) I have tried: d = new Date(); $("#image_test").attr("src", data+d.getTime()); , $("#i ...

Accessing information from Firebase and displaying it within an Angular Controller

As a newcomer to the latest Firebase SDK (with some experience using angularfire), I set out to retrieve data and display it using Angular. This is my progress so far: var app = angular.module('dApp',[]); app.controller('listingControler&a ...

What is the best way to divide data using [/ -]?

I am trying to split a string that shows a date. The date is shown in the format dd/mm/yyyy or dd-mm-yyyy. This is my code (the current string is formatted as mm/dd/yyyy): <input type="data" id="dat"> ..... var dataString=$('#dat').val(); ...

Guide to iterating through a queue of promises (sequentially handling asynchronous messages)

Currently, I am working on processing a queue of promises (which are representations of messages) in a specific order and using AngularJS for the task. Just to give you an idea, let's say that I have a method called connect() which returns a promise ...

"NodeJS Express: The intricacies of managing overlapping routers

While constructing a NodeJS express API, I have encountered a peculiar bug. It seems that some of the endpoints are overlapping, causing them to become unreachable as the request never completes and ends up timing out. For example: const load_dirs = (dirs ...

pass user identification by using a link using php and MySQL

In my table of items, there is an option for users to delete or restore each item, using a special PHP file called 'delete.php'. print "<td>"; if ($row['deleted'] == 'y') { print "<a href='delete.php?id=2'& ...

"Combining Angular and jQuery for powerful web development

I am looking to develop an application using Angular and incorporate numerous jQuery animations and jQuery UI effects (such as animate, fadeIn, fadeOut, etc). Is it feasible to use jQuery functions in conjunction with Angular? ...

Animating a div using jQuery to scroll across the screen and then return

I've been inspired by a feature on the Ben the Bodyguard website and I'm trying to figure out how it's done. From what I can tell, it involves some kind of loop, but I'm not exactly sure how to execute it. The specific part I'm re ...

Determining html column values using a related column and user input

Is there a way to populate an HTML table column (using Javascript or jQuery) based on the values in another column and an input field? For instance, if I input the number 115 into the field, then the advance column should display a value of 1 for each ath ...

Expanding the height of an image in a multi-select dropdown using CSS

I have an image that is included in all text fields and drop downs like so: However, when it comes to a multiple select drop down box, the image appears differently: I would like the image to display as shown above for multiple select drop downs. I atte ...

Attach a click event to choose an option in the dropdown

I'm facing an issue trying to get a click event working on an HTML option element. Even something as simple as console.log('hello!'); doesn't seem to be functioning. This is the code I've been using: $('#mySelect option&apo ...

The "loose" mode is not resolving the issue with the lack of support for the experimental syntax 'classProperties' that is currently disabled

Error Message: The experimental syntax 'classProperties' is currently not enabled Even after trying the suggested solutions, I still encounter the error during re-building. Click here for more information on the experimental syntax 'classP ...

What is the best way to check for a matching array value in my menu list and apply a corresponding class tag to it?

I need a way to dynamically add a class to tags that match specific array values. My menu list consists of 150 items, and I want to add a class to the tag whose text matches an element in the array. <ul class="nav main" id="tabs"&g ...