Adding Close Button In Div To Close The Box
Solution 1:
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);
returnfalse;
};
};
Css for close button
#close {
float:right;
display:inline-block;
padding:2px5px;
background:#ccc;
}
You may add a hover effect like
#close:hover {
float:right;
display:inline-block;
padding:2px5px;
background:#ccc;
color:#fff;
}
Something like this one.
Solution 2:
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">
thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etcthis is a short description yada yada peanuts etc
</p>
</div>
</a>
</div>
Solution 3:
You can use this jsFiddle
And HTML:
<div id="previewBox">
<button id="closeButton">Close</button>
<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">
thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etcthis is a short description yada yada peanuts etc
</p>
</div>
</a>
</div>
With JS (jquery required):
$(document).ready(function() {
$('#closeButton').on('click', function(e) {
$('#previewBox').remove();
});
});
Solution 4:
Here's the updated FIDDLE
Your HTML should look like this (I only added the button):
<a class="fragment" href="google.com">
<button id="closeButton">close</button>
<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">
thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etc thisis a short description yada yada peanuts etcthis is a short description yada yada peanuts etc
</p>
</div>
</a>
and you should add the following CSS:
.fragment {
position: relative;
}
#closeButton {
position: absolute;
top: 0;
right: 0;
}
Then, to make the button actually work, you should add this javascript:
document.getElementById('closeButton').addEventListener('click', function(e) {
e.preventDefault();
this.parentNode.style.display = 'none';
}, false);
We're using e.preventDefault()
here to prevent the anchor from following the link.
Solution 5:
A jQuery solution: Add this button inside any element you want to be able to close:
<button type='button' class='close' onclick='$(this).parent().remove();'>×</button>
or to 'just' hide it:
<button type='button' class='close' onclick='$(this).parent().hide();'>×</button>
Post a Comment for "Adding Close Button In Div To Close The Box"