Qadeer Mangrio Qadeer Mangrio - 3 months ago 11
CSS Question

How to apply jQuery on css selector :before

Here is my scenario, Created a

div
with
CSS
and added a
X
on top-right with before
pseudo-class
. How can I close this div by clicking on X with the help of
javascript
?

Image

CSS

.validation-summary-errors{
position: absolute;
width: 260px;
padding: 20px;
box-sizing: border-box;
top: 50%;
left: 50%;
background-color: #fff;
text-align: center;
font-size: 14px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
border-radius: 4px;
border: #000 solid 1px;
box-shadow: #232323 1px 1px 6px;
}

.validation-summary-errors:before {
display: block;
content: "X";
position: absolute;
right: -15px;
top: -15px;
background-color: rgba(0, 0, 0, 0.85);
padding: 5px;
border-radius: 50%;
font-size: 16px;
color: #fff;
width: 29px;
box-sizing: border-box;
}


HTML in View (Edit)

<div class="message-error">

@validationSummary

</div>

Answer

:before is an pseudo element and cannot be accessed in the DOM. Instead have a span or div with those style and use it's click event.

Note: Just make sure that you defined the CSS for span.close :-)

Solution 1: If you can edit the HTML.

$(function() {
  $('.close').click(function() {
    $(this).parent().hide();
  });
});
.validation-summary-errors {
  position: absolute;
  width: 260px;
  padding: 20px;
  box-sizing: border-box;
  top: 50%;
  left: 50%;
  background-color: #fff;
  text-align: center;
  font-size: 14px;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  border-radius: 4px;
  border: #000 solid 1px;
  box-shadow: #232323 1px 1px 6px;
}
span.close {
  cursor: pointer;
  display: block;
  content: "X";
  position: absolute;
  right: -15px;
  top: -15px;
  background-color: rgba(0, 0, 0, 0.85);
  padding: 5px;
  border-radius: 50%;
  font-size: 16px;
  color: #fff;
  width: 29px;
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="validation-summary-errors">
  This specified email already exists
  <span class="close">X</span>
</div>

Solution 2: If HTML is created dynamically.

$(function() {
  $('.validation-summary-errors').append('<span class="close">X</span>');

  $('.validation-summary-errors').on('click', 'span.close', function() {
    $(this).parent().hide();
  });
});
.validation-summary-errors {
  position: absolute;
  width: 260px;
  padding: 20px;
  box-sizing: border-box;
  top: 50%;
  left: 50%;
  background-color: #fff;
  text-align: center;
  font-size: 14px;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  border-radius: 4px;
  border: #000 solid 1px;
  box-shadow: #232323 1px 1px 6px;
}
span {
  cursor: pointer;
  display: block;
  content: "X";
  position: absolute;
  right: -15px;
  top: -15px;
  background-color: rgba(0, 0, 0, 0.85);
  padding: 5px;
  border-radius: 50%;
  font-size: 16px;
  color: #fff;
  width: 29px;
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="validation-summary-errors">
  This specified email already exists
</div>