Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  36] [ 7]  / answers: 1 / hits: 46657  / 10 Years ago, wed, february 26, 2014, 12:00:00

So I'm trying to write a super simple script that will allow a user to throw any link or button with the class .close inside of a div, and when that .close link is clicked, it automatically closes the parent container.



Here is what I'm currently trying to work with: JSFiddle



The code that I am currently trying to use is:



HTML



<div class=well notice bg-green>
<button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
<p>This is a notice that is green.</p>
</div>


CSS



.well {
background: #f9f9f9;
border-color: #f1f1f1;
border-style: solid;
border-width: 1px;
padding: 15px 20px;
}
.notice {
margin: 15px 0;
padding: 0px 15px;
}
.well.bg-green {
background: #dff0d8;
border-color: #d6e9c6;
color: #468847;
}
.close {
color: #000;
filter: alpha(opacity=20);
float: right;
font-size: 21px;
font-weight: bold;
line-height: 1;
margin-top: 15px;
opacity: .2;
text-shadow: 0 1px 0 #fff;
}
.close:hover, .close:focus {
color: #000;
cursor: pointer;
filter: alpha(opacity=50);
opacity: .5;
text-decoration: none;
}
button.close {
background: transparent;
border: 0;
cursor: pointer;
padding: 0;
-webkit-appearance: none;
-moz-appearance: none;
}


JavaScript (jQuery)



$('.close').live(click, function () {
$(this).parents('div').fadeOut;
});


Let me know if my question doesn't make sense or if any more elaboration is needed. Thank you!


More From » jquery

 Answers
2

Two problems:




  • live() doesn't exist in the version of jQuery in your fiddle (deprecated in 1.7, removed in 1.9)

  • fadeOut is a function (you were missing parens to execute it)



http://jsfiddle.net/KF7S6/



$('.close').on(click, function () {
$(this).parents('div').fadeOut();
});


If you want it to work on dynamic elements, use this version:



$(document).on('click', '.close', function () {
$(this).parents('div').fadeOut();
});

[#72290] Tuesday, February 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;