Remove a specific DIV in a row by its ID
I am using Django and have several Bootstrap cards on my page like the structure below, and I am trying to remove one div on click (X on my <a>
)
<row>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 1
</>
</>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 2
</>
</>
</>
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
var $this = $(this);
var id = $this.data('id');
$.get('/messages/delete/'+id, , function(response)
$this.parent().fadeOut(300, function()
$(this).remove();
);
);
);
);
The on click doesn't work. Debugging the code:
127.0.0.1:8000/messages/delete/1 404 NOT FOUND
I understand, the "a" has a connection to my function. What I don't understand, whats the purpose of
$.get('/messages/delete/'+id, , function(response)?
I just want the Card-Div to be removed.
How can I change my code to make it work? Any input appreciated.
javascript jquery html
add a comment |
I am using Django and have several Bootstrap cards on my page like the structure below, and I am trying to remove one div on click (X on my <a>
)
<row>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 1
</>
</>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 2
</>
</>
</>
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
var $this = $(this);
var id = $this.data('id');
$.get('/messages/delete/'+id, , function(response)
$this.parent().fadeOut(300, function()
$(this).remove();
);
);
);
);
The on click doesn't work. Debugging the code:
127.0.0.1:8000/messages/delete/1 404 NOT FOUND
I understand, the "a" has a connection to my function. What I don't understand, whats the purpose of
$.get('/messages/delete/'+id, , function(response)?
I just want the Card-Div to be removed.
How can I change my code to make it work? Any input appreciated.
javascript jquery html
1
Are you also deleting a row from a database? Because it's an ajax call, also you should never use GET in this case, POST or DELETE is the way to go
– Tomasz Rup
Nov 13 '18 at 16:29
1
The element isn't being removed because the callback is only executed when the AJAX request is successful. You're getting a 404 response, so that's not happening.
– Rory McCrossan
Nov 13 '18 at 16:31
add a comment |
I am using Django and have several Bootstrap cards on my page like the structure below, and I am trying to remove one div on click (X on my <a>
)
<row>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 1
</>
</>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 2
</>
</>
</>
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
var $this = $(this);
var id = $this.data('id');
$.get('/messages/delete/'+id, , function(response)
$this.parent().fadeOut(300, function()
$(this).remove();
);
);
);
);
The on click doesn't work. Debugging the code:
127.0.0.1:8000/messages/delete/1 404 NOT FOUND
I understand, the "a" has a connection to my function. What I don't understand, whats the purpose of
$.get('/messages/delete/'+id, , function(response)?
I just want the Card-Div to be removed.
How can I change my code to make it work? Any input appreciated.
javascript jquery html
I am using Django and have several Bootstrap cards on my page like the structure below, and I am trying to remove one div on click (X on my <a>
)
<row>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 1
</>
</>
<col-sm-3 id=" topic.pk ">
<card>
<...><a href="" class="remove" data-id=" topic.pk ">X</a></> #ID here is 2
</>
</>
</>
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
var $this = $(this);
var id = $this.data('id');
$.get('/messages/delete/'+id, , function(response)
$this.parent().fadeOut(300, function()
$(this).remove();
);
);
);
);
The on click doesn't work. Debugging the code:
127.0.0.1:8000/messages/delete/1 404 NOT FOUND
I understand, the "a" has a connection to my function. What I don't understand, whats the purpose of
$.get('/messages/delete/'+id, , function(response)?
I just want the Card-Div to be removed.
How can I change my code to make it work? Any input appreciated.
javascript jquery html
javascript jquery html
edited Nov 13 '18 at 17:02
Pete
40.9k1876116
40.9k1876116
asked Nov 13 '18 at 16:26
JohnDoleJohnDole
356
356
1
Are you also deleting a row from a database? Because it's an ajax call, also you should never use GET in this case, POST or DELETE is the way to go
– Tomasz Rup
Nov 13 '18 at 16:29
1
The element isn't being removed because the callback is only executed when the AJAX request is successful. You're getting a 404 response, so that's not happening.
– Rory McCrossan
Nov 13 '18 at 16:31
add a comment |
1
Are you also deleting a row from a database? Because it's an ajax call, also you should never use GET in this case, POST or DELETE is the way to go
– Tomasz Rup
Nov 13 '18 at 16:29
1
The element isn't being removed because the callback is only executed when the AJAX request is successful. You're getting a 404 response, so that's not happening.
– Rory McCrossan
Nov 13 '18 at 16:31
1
1
Are you also deleting a row from a database? Because it's an ajax call, also you should never use GET in this case, POST or DELETE is the way to go
– Tomasz Rup
Nov 13 '18 at 16:29
Are you also deleting a row from a database? Because it's an ajax call, also you should never use GET in this case, POST or DELETE is the way to go
– Tomasz Rup
Nov 13 '18 at 16:29
1
1
The element isn't being removed because the callback is only executed when the AJAX request is successful. You're getting a 404 response, so that's not happening.
– Rory McCrossan
Nov 13 '18 at 16:31
The element isn't being removed because the callback is only executed when the AJAX request is successful. You're getting a 404 response, so that's not happening.
– Rory McCrossan
Nov 13 '18 at 16:31
add a comment |
1 Answer
1
active
oldest
votes
Extending my comment.
This part:
$.get('/messages/delete/'+id, , function(response) ... );
is an AJAX call (that also uses the GET HTTP method - and it should use either POST or DELETE) that sends a request to the specified route. One could guess that it's connected to a delete method, to remove a row with specified ID from a database.
Your error comes up, because you don't have that route set up.
This part:
$this.parent().fadeOut(300, function(){$(this).remove()
is the one that actually removes the HTML element from the page. In your current code it's called in a callback function from the AJAX request (that never succeeds), therefore it never fires.
If you only want to remove the card element from the html, you should go with:
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
$(this).closest('card').remove();
);
);
If you actually want to remove a row from a table in your database that would require you setting up a route and a method in the backend (Django in your case), and then removing it in the frontend after a successful request.
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285368%2fremove-a-specific-div-in-a-row-by-its-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Extending my comment.
This part:
$.get('/messages/delete/'+id, , function(response) ... );
is an AJAX call (that also uses the GET HTTP method - and it should use either POST or DELETE) that sends a request to the specified route. One could guess that it's connected to a delete method, to remove a row with specified ID from a database.
Your error comes up, because you don't have that route set up.
This part:
$this.parent().fadeOut(300, function(){$(this).remove()
is the one that actually removes the HTML element from the page. In your current code it's called in a callback function from the AJAX request (that never succeeds), therefore it never fires.
If you only want to remove the card element from the html, you should go with:
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
$(this).closest('card').remove();
);
);
If you actually want to remove a row from a table in your database that would require you setting up a route and a method in the backend (Django in your case), and then removing it in the frontend after a successful request.
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
add a comment |
Extending my comment.
This part:
$.get('/messages/delete/'+id, , function(response) ... );
is an AJAX call (that also uses the GET HTTP method - and it should use either POST or DELETE) that sends a request to the specified route. One could guess that it's connected to a delete method, to remove a row with specified ID from a database.
Your error comes up, because you don't have that route set up.
This part:
$this.parent().fadeOut(300, function(){$(this).remove()
is the one that actually removes the HTML element from the page. In your current code it's called in a callback function from the AJAX request (that never succeeds), therefore it never fires.
If you only want to remove the card element from the html, you should go with:
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
$(this).closest('card').remove();
);
);
If you actually want to remove a row from a table in your database that would require you setting up a route and a method in the backend (Django in your case), and then removing it in the frontend after a successful request.
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
add a comment |
Extending my comment.
This part:
$.get('/messages/delete/'+id, , function(response) ... );
is an AJAX call (that also uses the GET HTTP method - and it should use either POST or DELETE) that sends a request to the specified route. One could guess that it's connected to a delete method, to remove a row with specified ID from a database.
Your error comes up, because you don't have that route set up.
This part:
$this.parent().fadeOut(300, function(){$(this).remove()
is the one that actually removes the HTML element from the page. In your current code it's called in a callback function from the AJAX request (that never succeeds), therefore it never fires.
If you only want to remove the card element from the html, you should go with:
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
$(this).closest('card').remove();
);
);
If you actually want to remove a row from a table in your database that would require you setting up a route and a method in the backend (Django in your case), and then removing it in the frontend after a successful request.
Extending my comment.
This part:
$.get('/messages/delete/'+id, , function(response) ... );
is an AJAX call (that also uses the GET HTTP method - and it should use either POST or DELETE) that sends a request to the specified route. One could guess that it's connected to a delete method, to remove a row with specified ID from a database.
Your error comes up, because you don't have that route set up.
This part:
$this.parent().fadeOut(300, function(){$(this).remove()
is the one that actually removes the HTML element from the page. In your current code it's called in a callback function from the AJAX request (that never succeeds), therefore it never fires.
If you only want to remove the card element from the html, you should go with:
$(function()
$('a.remove').on('click', function(e)
e.preventDefault();
$(this).closest('card').remove();
);
);
If you actually want to remove a row from a table in your database that would require you setting up a route and a method in the backend (Django in your case), and then removing it in the frontend after a successful request.
edited Nov 13 '18 at 16:41
answered Nov 13 '18 at 16:37
Tomasz RupTomasz Rup
45118
45118
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
add a comment |
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
Based on your old comment, I did something like this: $(function() $('a.remove').on('click', function(e) e.preventDefault(); var $this = $(this); var id = '"#' + $this.data('id') + '"'; $(id).remove(); ); ); - which results in Uncaught Error: Syntax error, unrecognized expression: "#8". Your last piece of code does nothing (not even an error) - I wont say I am stupid, but I have REAL problems which JavaScript. Thank you for your input anyway
– JohnDole
Nov 13 '18 at 16:41
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
try $('#' + $(this).data('id')).remove();
– Tomasz Rup
Nov 13 '18 at 16:43
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
THANK YOU SO MUCH TOMASZ!
– JohnDole
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
im glad it works :)
– Tomasz Rup
Nov 13 '18 at 16:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285368%2fremove-a-specific-div-in-a-row-by-its-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you also deleting a row from a database? Because it's an ajax call, also you should never use GET in this case, POST or DELETE is the way to go
– Tomasz Rup
Nov 13 '18 at 16:29
1
The element isn't being removed because the callback is only executed when the AJAX request is successful. You're getting a 404 response, so that's not happening.
– Rory McCrossan
Nov 13 '18 at 16:31