Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding close button in div to close the box

I have created a URL preview box for the entered URL.

Preview is shown in the div box. I want to add a close option on the right top. How could be done so that when users click on its box should be disabled?

Here is my fiddle.

<a class="fragment" href="google.com">     <div>     <img src ="http://placehold.it/116x116" alt="some description"/>      <h3>the title will go here</h3>         <h4> www.myurlwill.com </h4>     <p class="text">         this is a short description yada yada peanuts etc this is a short description yada yada peanuts etc this is a short description yada yada peanuts etc this is a short description yada yada peanuts etcthis is a short description yada yada peanuts etc      </p> </div> </a> 

code is inside php

like image 806
user123 Avatar asked Oct 31 '13 10:10

user123


People also ask

How Add Close button to div?

In the function, we select the close button with document. getElementById('close') . And we set the onclick property to a function that removes the the parent of the close button, which is the div. We get the parent of the close button with this.

How do I create a close button in HTML?

A close button is a <button> element with the class . close-button . We use the multiplication symbol ( &times; ) as the X icon. This icon is wrapped in a <span> with the attribute aria-hidden="true" , so screen readers don't read the X icon.

How do I add a close button in CSS?

The task is to make the close button using Pure CSS. There are two approaches that are discussed below. Approach 1: Create a <div> inside another <div> which has alphabet 'X' in it which helps in displaying the close button. When the “click” event occurs on 'X', then perform the operation.

Can you put a button in a div?

Use the following steps to center align a button in a div container: Create a div container. Insert the button tag. In the CSS for the div set the text-align to center.


2 Answers

Most simple way (assumed you want to remove the element)

<span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span> 

Add this inside your div, an example here.

You may also use something like this

window.onload = function(){     document.getElementById('close').onclick = function(){         this.parentNode.parentNode.parentNode         .removeChild(this.parentNode.parentNode);         return false;     }; }; 

An Example Here.

Css for close button

#close {     float:right;     display:inline-block;     padding:2px 5px;     background:#ccc; } 

You may add a hover effect like

#close:hover {     float:right;     display:inline-block;     padding:2px 5px;     background:#ccc;     color:#fff; } 

Something like this one.

like image 88
The Alpha Avatar answered Sep 23 '22 10:09

The Alpha


it's easy with the id of the div container : (I didn't put the close button inside the <a> because that's does work properly on all browser.

<div id="myDiv"> <button class="close" onclick="document.getElementById('myDiv').style.display='none'" >Close</button> <a class="fragment" href="http://google.com">     <div>     <img src ="http://placehold.it/116x116" alt="some description"/>      <h3>the title will go here</h3>         <h4> www.myurlwill.com </h4>     <p class="text">         this is a short description yada yada peanuts etc this is a short description yada yada peanuts etc this is a short description yada yada peanuts etc this is a short description yada yada peanuts etcthis is a short description yada yada peanuts etc      </p> </div> </a> </div> 
like image 30
Asenar Avatar answered Sep 22 '22 10:09

Asenar