0

Playing with JavaScript, I have a search function which gets information from a database and then uses JavaScript to add the elements to my site in a box with a cross near the element. I am trying to make it so that when the user presses the X, near the element, they can remove it, if they made a mistake.

  $('#addButton').on('click', function(event) {
    var searchedValue = $('#search_term').val();
    var divHolder = $('.selectedStuff');
    $("<div>" + searchedValue + "</div>").css({
      'background-color': 'yellow',
      'width': '700px',
      'margin-top': '10px',
      'border-style': 'solid',
      'border-color': '#0000ff'
    }).appendTo(divHolder);

So, I tried certain methods, but I cant seem to get it to work. I have commented out the bit. Again, it's just when the user clicks on the X that element will be deleted.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nevershow2016
  • 570
  • 6
  • 19

5 Answers5

2

You need to change your html because you can't bind click event with "::after" as it's is not html element. So rather then adding css on "::after" add span in place of that and apply CSS on it.

$(document).ready(function() {
  $('.searchFunction').keyup(function(event) {
    var search_term = $("#search_term").val();

    $.post(document.location.href, {
      search_term: search_term
    }, function(data) {
      $('.result').html(data);
    });
  });

  $('.result').on('click', 'li', function(event) {
    var result_value = $(this).text();
    $('#search_term').val(result_value);
    $('.result').html('');
  });

  $('#addButton').on('click', function(event) {
    var searchedValue = $('#search_term').val();
    var divHolder = $('.selectedStuff');
    $("<div>" + searchedValue + "<span>X</span></div>").css({
      'background-color': 'yellow',
      'width': '700px',
      'margin-top': '10px',
      'border-style': 'solid',
      'border-color': '#0000ff'
    }).appendTo(divHolder);

     $('.selectedStuff span').on("click", function(){
       $(this).closest("div").remove();  
     });
  });
});
.selectedStuff > div {
  width: 300px;
}
.selectedStuff span {
  font-family: Arial;
  color: red;
  position: relative;
  float: right;
  right: -20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name='search_term' id="search_term" class="searchFunction">
<input id="addButton" type="button" value="Add">
<div class="dropdown">
  <ul class="result"></ul>
</div>
<div class="selectedStuff"></div>
<div id="markx"></div>
Ranish Karim
  • 366
  • 1
  • 8
0

Based on your information I suggest you need use remove method:

$('.selectedStuff > div:after').on("click", function() {
  $(element_to_delete).remove();
}
Arsenowitch
  • 401
  • 5
  • 22
0
$('.selectedStuff > div:after').on("click", function() {
  $(this).remove();
}
aKiRa
  • 116
  • 1
  • 5
0

Add the x into another position, because you cannot detect events in pseudo-elements like :after.

X is put in the preferred position via css

Result: jsfiddle

  $('#addButton').on('click', function( event ) {
        var searchedValue = $('#search_term').val(); 
        var divHolder = $('.selectedStuff'); 
    $("<div ><div class='bar'>" + searchedValue + "</div><div class=\"close\">X</span></div>").css({

        }).appendTo(divHolder); 

     $('.close').on("click", function() {
            $(this).parent().remove();
    });
    });
    });


.selectedStuff > div{
  width:500px;
  display: table-row;
}

.close{
   display: table-cell; 
   font-family: Arial;
   color: red;
   position:relative;
   right:-20px;
 }

 .bar{
        background-color: yellow;
        width: 400px;
        margin-top: 10px;
        border-style: solid;
        border-color: #0000ff;
        display: table-cell;


        }
silviagreen
  • 1,679
  • 1
  • 18
  • 39
0

Check this answer: Only detect click event on pseudo-element

TL;DR You can't bind an event on pseudo-element, just insert x to the DOM (e.g. as a div) and bind event on it.

Community
  • 1
  • 1
Max Filippov
  • 2,024
  • 18
  • 37