I am trying to create a delete button which will delete its parent div and everything inside it
function remove() {
var $this = $(this).closest();
//console.log($this)
$this.remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="background-color: green;">
<div>NAME</div>
<div><button onclick="remove">Remove</button>
</div>
</div>
However, whenever I press the button, nothing happens.
EDIT:
$(function() {
$('button').click(function() {
var $this = $(this).closest('.container');
$this.remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div>NAME 1</div>
<div><button>Remove</button></div>
</div>
<div class="container">
<div>NAME 2</div>
<div><button>Remove 2</button></div>
</div>
Nothing happens and no errors occur.
EDIT 2:
This is my entire javascript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script type="text/javascript">
var csrftoken = Cookies.get('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
var suggested_students = $("#suggested_students");
var search_bar = $('#search_bar');
function clear(){
suggested_students.empty();
search_bar.val('');
}
search_bar.keyup(function () {
var keyword = search_bar.val();
if(keyword.length > 2){
//console.log('hey')
var url = window.location.pathname;
$.ajax({
method: 'GET',
url: url,
data: {
'keyword': keyword,
},
dataType: 'json',
success: function (data) {
suggested_students.empty();
var suggestions = data.students;
for(i = 0; i< suggestions.length; i++){
var current_student = suggestions[i];
var div = $("<a href='#'><div/></a>");
div.data("name", current_student[0]);
div.data("id", current_student[1]);
div.html(current_student[0] + "<br/>");
div.on("click", moveProfile);
div.css({"background-color": "green"});
suggested_students.append(div);
}
}
})
}else if(keyword.length == 0){
clear()
}
})
var students_to_add = $("#students_to_add")
function moveProfile(){
var $this = $(this);
var studentID = $this.data("id");
var studentName = $this.data("name");
var div = $("<div/>");
div.data('id', studentID);
div.data('name', studentName);
div.html("<div>" + studentName +"</div><div><button>Remove</button></div>");
div.css({"background-color": "green"});
div.addClass("container")
students_to_add.append(div);
clear()
}
$(function() {
$('button').click(function() {
var $this = $(this).closest('.container');
$this.remove();
});
});
</script>
And this is the entire HTML:
<div>
<label>Set Name: </label>
<input type="text" class="class_name" id='class_name'/>
</div>
<br>
<div class='student_search_bar'>
<label for='search_bar'>Search for Students: </label>
<input type="text" class="student_search_bar_bar" id='search_bar'/>
</div>
<br>
<div id='suggested_students'>
</div>
<div id='students_to_add'>
<label>Students to add: </label>
</div>
I have a function which gets a list from an AJAX call and make it into multiple divs like so:
<div class="container">
<div>NAME</div>
<div>
<button>Remove</button>
</div>
</div>
My Javascript is below my html, and is all in the same file.
When I run the code that has been suggested by Rory McCrossan, I get no errors in the console, and when I have used a console.log
to see if the function is called, nothing is printed.