What is a way to close a message box (div) when clicking outside using only HTML code, as jQuery is not allowed on the page?

Can anyone assist me in finding a way to close a DIV (message box) when clicked outside of it? Additionally, the current buttons are not aligned in the same line. Is there a code available to align all buttons in one line?

I am looking for a solution that does not involve using jQuery as I can only implement HTML code on my page. Any help would be appreciated!

.alert {
display: none;
padding: 20px;
background-color: #f44336;
color: white;
}

.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
<button onclick="document.getElementById('alert1').style.display='block'">Button1</button> <div id="alert1" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 1 </div>

<button onclick="document.getElementById('alert2').style.display='block'">Button2</button> <div id="alert2" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 2 </div>

<button onclick="document.getElementById('alert3').style.display='block'">Button3</button> <div id="alert3" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 3 </div>

<button onclick="document.getElementById('alert4').style.display='block'">Button4</button> <div id="alert4" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 4 </div>

Answer №1

Here is a simple and efficient solution to address both of your inquiries:

1) Place the "alerts" in a separate div below the buttons to maintain alignment.

2) Include

onblur = "document.getElementById('alertxxxxxx').style.display='none'"

for your button elements.

.alert {
  display: none;
  padding: 20px;
  background-color: #f44336;
  color: white;
}

.closebtn {
  margin-left: 15px;
  color: white;
  font-weight: bold;
  float: right;
  font-size: 22px;
  line-height: 20px;
  cursor: pointer;
  transition: 0.3s;
}
<button onclick="document.getElementById('alert1').style.display='block'" onblur="document.getElementById('alert1').style.display='none'">Button1</button>
<button onclick="document.getElementById('alert2').style.display='block'" onblur="document.getElementById('alert2').style.display='none'">Button2</button>
<button onclick="document.getElementById('alert3').style.display='block'" onblur="document.getElementById('alert3').style.display='none'">Button3</button>
<button onclick="document.getElementById('alert4').style.display='block'" onblur="document.getElementById('alert4').style.display='none'">Button4</button>
<div>
  <div id="alert1" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 1 </div>
  <div id="alert2" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 2 </div>
  <div id="alert3" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>Message 3 </div>
  <div id="alert4" class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times</span>Message 4 </div>
</div>

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

The update query appears to be malfunctioning

I am currently working on implementing Add, Update, and Delete functionalities in PHP. Everything seems to be functioning well, except for my Update query which is causing issues. Can someone please assist me in identifying the error in my Update query? ...

Implement CSS styling within XML responses in jQuery UI autocomplete

Can you apply a different CSS class to a specific part of the label in the XML response? Currently, the label value is: label: $( "name", this ).text() + ", " + ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ) + ", " + $( "countryCode" ...

Stripping away all HTML elements, including their contents, from the text

Can anyone help me figure out how to remove all HTML tags and their contents using the BeautifulSoup library? Here is an example input: ... text <strong>ha</strong> ... text And this is the desired output: ... text ... text ...

Launch a PowerPoint presentation in a separate tab on your web browser

If you have a link that leads to a .pdf file, it will open in a new tab like this: <a target="_blank" href="http://somePDF.pdf">Download</a> But what about a Powerpoint file, such as a .ppt? Is there a way to make it open in a new browser tab ...

CSS Transform - Browser compatibility with animation timing

My CSS animations are working perfectly on WebKit browsers like Safari and Chrome, but the timing is all messed up on Firefox. JSFiddle: http://jsfiddle.net/jmorais/p5XCD/1/ Here is the code snippet: var open = false; var intransition = false; fun ...

Transferring information from website form to a C# program

Someone suggested using the HTTPListener class, but it appears to only return another web page in response. I need guidance on how to post data from a form on my website and process it in my C# application. If anyone has tutorials or simple examples, the ...

Removing modules that are loaded lazily in Angular 5

Is it possible to unload all previously used modules when a user logs out of the website in order to reset the application? I have noticed that when a new user logs into the backend server with the same running application, all lazy loaded modules are stil ...

Is there a way to incorporate HTML input text into an image with CSS?

Here is an image that I have: https://i.sstatic.net/wHNk1.png Is there a way to add input fields to this image? I want the user to be able to click on the input fields (green boxes) within the image and enter values. How can I achieve this? ...

Error retrieving resource: server returned a 404 status code indicating file not found for javaScript and CSS files

I can't seem to get my css and js files to load on the server. Here is how my file structure looks like: GAME_Folder https://i.sstatic.net/wzfB3.png HTML doctype html head link(href='https://fonts.googleapis.com/css2?family=Press+Start+2P& ...

I am attempting to format an HTML form with proper indentation, but it seems to be eluding me

Struggling to design a custom registration form template, I'm facing an issue where paragraph spaces or indents are not being recognized by the program, even in the preview pane. Seeking assistance with this problem. I am attempting to position this ...

Successfully launched Laravel application, experiencing issues with all pages except for the homepage

I recently completed a website for a client and everything seems to be working fine locally. Unfortunately, when trying to access any links on the site, I am getting a 500 server error. The hosting is with 1and1, and I've heard mixed reviews about it ...

Embedding an image within an HTML textbox using the textboxfor method

Looking to elevate the aesthetic of my project, I decided to incorporate a watermark into my @html.textboxfor. However, despite successfully adding it, I encountered an issue with compatibility on IE 8. As a solution, I aim to include an image within the @ ...

Tips for aligning the child elements of a dropdown menu to stick with the parent's top position

It's important that progress and my goals are clearly outlined here: View my CodePen When it comes to the "Offices" section, I need the child objects to display directly below and ideally match the width of the parent element. Here is the code snip ...

Incorporating custom HtmlHelper methods in ASP.NET MVC 3

As a newcomer to MVC, I am eager to create my own extension method in order to enhance the functionality of the html helpers available in my razor views. While Html.DropDownListFor() allows for the creation of a drop down list for any property on my model, ...

"Any tips on maintaining the blue color of my dropdown link when it is selected as active

I'm struggling to keep the bootstrap accordion dropdown link blue when it's active. I've tried different methods but none seem to work. What am I missing? Here's my code: <div class="visible-sm visible-xs" id="accordion" role="t ...

Elements centered within a flex container are expanding and overflowing above

It seems like I'm missing a key point when it comes to my flexbox setup for vertical and horizontal centering. The issue arises when the container's height exceeds that of its parent, causing the content to extend beyond the top but remain fixed ...

How can I retrieve the position of a div or an image within a div (specifically the top and left coordinates) and incorporate these values into CSS?

I'm currently working on a website that is dynamic and utilizes bootstrap. I am looking to incorporate an animation where the dynamic thumbnails in the col-md-4 div zoom to 100% when clicked, appearing at the center of the container. I am struggling ...

Tips on adjusting the placement of a div element with JavaScript

Initially, I am unsure if the title accurately reflects what I am seeking. Within my HTML form, I have numerous elements such as div, p, or js. Here is an example: <div id="main"> <div id="abc1"></div> <div id="abc2"></div&g ...

Unable to move to a different HTML page following validation with JavaScript

Recently, I completed a HTML page that consists of two separate sections - one for logging in and the other to display after a successful login. In order to ensure proper validation on the login.html page, I implemented the following: However, when attemp ...

Prevent the function from running more than once

I need a specific function function toDiv() { $(".wrap"))) $('.barfill').each(function(){ var width=$(this).data('width'); $(this).animate({ width: width }, 500); var $perctext = $('<div>', ...