Hiding and securing a div element when JavaScript is disabled

My form includes a <div>.

This particular <div> is a red circle. Even when JavaScript is disabled, it remains visible (I can use display: none to address this).

The issue arises when I re-enable JS - upon user interaction, the <div> moves down a line (it does not stay in its original position).

What bothers me is that I have input fields and the div is supposed to be at the end of each one... So what I would like (if possible) is for the div to remain hidden when JS is off and not jump down a line when JS is on and there is user interaction...

HTML:

  <!DOCTYPE html>
  <html>
  <head>
  <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/jquery-3.0.0-alpha1.js"></script>
<meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>

 <div id="name1" >
         <input type="text" id="namebox" placeholder="First name* ..."name="name">
         <div class="span1">!</div>
         </div> 
         <div id="name2" >
         <input type="text" id="namebox1" placeholder="Last name* ..." name="surname">
          <div class="span1">!</div>
         </div> 
  <script src="http://code.jquery.com/jquery-1.11.3.min.js"  type="text/javascript" charset="utf-8"></script>

</body>

</html>

CSS:

 .span1{
 background-color: white;
 color: red;
 height: 28px;
 width: 28px;
 font-size: 0.6em;
 margin-right: 1px;
 margin-bottom:2px;
 line-height: 30px;
 text-align: center;
 text-shadow: 0 1px 0 rgba(255,255,255,0.2);
 border-radius: 100%;
 border:2px solid red;
 display:inline-block;
 font-family:Verdana, Geneva, sans-serif;
 }

JavaScript:

$(".span1").hide();

function nameEvent(){
$(this).next().toggle($(this).val().length <=1);
}
$("#namebox").on("focus input", nameEvent);

Answer №1

Remember to insert the following code into your CSS file

#name{
position:relative;
}

Additionally, include the property position:absolute; within the .span1 class

This will result in the .span1 class looking like this:

.span1{
background-color: white;
color: red;
height: 28px;
width: 28px;
font-size: 0.6em;
margin-right: 1px;
margin-bottom:2px;
line-height: 30px;
text-align: center;
text-shadow: 0 1px 0 rgba(255,255,255,0.2);
border-radius: 100%;
border:2px solid red;
display:inline-block;
font-family:Verdana, Geneva, sans-serif;
position:absolute;
}

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

How can I adjust the padding and width attributes of the mat-menu in Angular 5

Today, I am struggling with a piece of code. Whenever I click on the Details button, it is supposed to open a mat-menu. However, for some reason, I cannot seem to adjust the padding or width of the menu: <div id="box-upload" [hidden]="hiddenBoxUpload" ...

Using Styled Components to achieve full width for input tag in relation to its parent

I am working with an input field that has a specific width set. My goal is to increase the width of this input field to 100% by selecting it from its parent component. Is there a way to achieve this without passing an explicit width prop in the styled c ...

What is the best way to shorten a text using ellipses based on character count, rather than line breaks

If I have a text that needs to be trimmed down to 15 characters, like "I want to trim this text into 15 characters for example," the result should be something like "I want to limit..." in my email template. Is there a way to achieve this using just CSS? ...

The current status of Dropzone.js is in a pending state, preventing the upload of any

I have integrated Multer in the back-end for file upload handling and Dropzone.js in the front-end. Testing my back-end code with Postman works perfectly, but when using Dropzone, the status remains pending and the file does not get uploaded. After waiting ...

Encountering challenges with Angular in creating a dynamically responsive background

I'm struggling to make this website fully responsive and adapt to various screen resolutions. The background images I'm using are high-resolution, but they don't seem to cover the entire page. CSS : @media screen and (max-width: 600px) { ...

What is the process for loading a webpage into another webpage with google app engine?

Hey there, I'm a newbie to app engine and could really use some help, I'm looking to replicate the functionality of the jquery load function on the server side within a RequestHandler's get() method. Currently, I have a page that looks some ...

What is the best way to eliminate the space between columns?

I have a simple structure on BootStrap. <div class="container"> <div class="row"> <div class="col-md-4 blue-section" ><p>Some content</p></div> <div class="col-md-4 red-section"><p>Some content&l ...

Node/Express API returning empty body when being accessed through fetch or axios requests

Currently working on integrating an API call in a React app using Node/Express. No matter where I place the fetch/axios call, the parsed body always shows up as undefined in my controller. Yesterday, I was experimenting with fetch but decided to switch to ...

Leverage jQuery to organize and categorize JSON information received through an Ajax call

I have retrieved the following array from a MySQL database using PDO: [{ "tbl":"1", "orid":"915", "date":"2021-12-30 12:46:48", "flag":0 }, { "tbl":"2", "orid":"914", "date":"2021-12-30 12:46:21", "flag ...

Learn how to create a logarithmic scale graph using CanvasJS by fetching data from an AJAX call

window.onload = function() { var dataPoints = []; // fetching the json data from api via AJAX call. var X = []; var Y = []; var data = []; function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("applicatio ...

Is there a link outside a webpage that has Facebook connect capabilities?

Recently, I started delving into the world of the Facebook API. My initial experiment was a simple "Hello World" application that displays the logged-in username. Everything was working perfectly when I had the FB.init() function inline - the "Login using ...

Execute the JavaScript [ window:print() ] function once the webpage has finished loading along with any

I am facing an issue with my web page that has around 10 Google Analytics charts. I want to trigger a print dialog box once the page is fully loaded, including all the charts and JavaScript. Currently, the problem is that the print dialog box opens after t ...

What causes the footer to disappear in Rails with Bootstrap?

Within my application.html.erb file, my setup is pretty standard: <html class="h-100"> !head, title, scripts, styles, stylesheets removed for this example here on stackoverflow! <body class="d-flex flex-column h-100"> <!-- Dropdown St ...

Can Jquery's 'on' method function properly on elements that are added with element.innerHTML?

On our website, we have a 'content' div where the HTML is inserted. For example: content.innerHtml = 'a large amount of HTML with divs and tables' Now, I am trying to add a jQuery event handler to one of the inserted elements by using ...

Prompting the website to 'refresh' and return to the beginning of the page

My website's functionality is closely tied to the user's position on the page. Different features are triggered once specific distances are reached. I am seeking a way for users to automatically return to the top of the page upon page reload. Cu ...

Determining when a text area has selected text without constant checking

class MarkdownEditor extends React.Component { constructor(props) { super(props); this.timer = null; this.startIndex = null; this.endIndex = null; } componentDidMount() { this.timer = setInterval(() => { this.setSelectio ...

Non-responsive sidebar

I am currently facing two issues with my sidebar. The top part gets cut off on mobile devices. The sidebar is expanded by default on mobile devices, which is not the desired behavior. Also, when it is collapsed, the content of the page does not shift to t ...

Removing Additional Parameters from the Delete URL in jqGrid

I'm currently working on adding delete, update, and add functionality to my grid. The problem I'm facing is that when I submit the delete confirmation popup, the URL ends up with extra parameters that I want to remove. For example: I want it to ...

What is the process for generating a tree structure from an HTML element?

This particular element is found within my Vue2 application: <div class="child-elements-container draggable-container"> <div> <div entity-type="entitytype1" type="elementType" id="2" class= ...

Issue with ISO-8859-1 character encoding in table formatting

I have set my website to use ISO-8859-1 encoding and special characters are displaying correctly. However, I'm facing an issue with the datatables plugin which does not seem to recognize special characters in the table data. Do I need to configure an ...