Adding button with onclick event in .append() with JQuery/Javascipt
I want to add delete button to every row where status is ORDERED. I have tried different variations. This solution could work without error "Cannot read property 'id' of undefined" even though data[i] is not undefined. Alert before .click() gives right id. What is wrong here? Can i do something differently or better here?
).done( (data, status, jqXHR) => {
if(data[0].order_username != undefined) {
$("#orders_table").empty();
for(var i = 0; i < data.length; i++) {
var button = "";
if(data[i].status == "ORDERED") {
button = "<td><button class='delete'>Delete</button></td>";
}
$("#orders_table").append(
"<tr>" +
"<td>" + data[i].order_username + "</td>"+
"<td>" + data[i].work_description + "</td>" +
"<td>" + dateFormatter(data[i].orderdate) + "</td>" +
"<td>" + dateFormatter(data[i].startdate) + "</td>" +
"<td>" + dateFormatter(data[i].readydate) + "</td>" +
"<td>" + dateFormatter(data[i].accepteddate) + "</td>" +
"<td>" + dateFormatter(data[i].denieddate) + "</td>" +
"<td>" + data[i].comment_of_work + "</td>" +
"<td>" + data[i].hours + "</td>" +
"<td>" + data[i].approx_budget + "</td>" +
"<td>" + data[i].status + "</td>" +
button +
"</tr>"
)
alert(data[i].id);
$(".delete").click(() => {
deleteUser(data[i].id);
})
}
}
function deleteUser(key) {
$.ajax(
{
url: "http:localhost:3001/workorders_delete/"+key,
method: 'delete'
}).done( (data, status, jqXHR) => {
}).fail( (jqXHR, status, errorThrown) => {
console.log("Call failed: "+errorThrown);
});
}
javascript jquery
add a comment |
I want to add delete button to every row where status is ORDERED. I have tried different variations. This solution could work without error "Cannot read property 'id' of undefined" even though data[i] is not undefined. Alert before .click() gives right id. What is wrong here? Can i do something differently or better here?
).done( (data, status, jqXHR) => {
if(data[0].order_username != undefined) {
$("#orders_table").empty();
for(var i = 0; i < data.length; i++) {
var button = "";
if(data[i].status == "ORDERED") {
button = "<td><button class='delete'>Delete</button></td>";
}
$("#orders_table").append(
"<tr>" +
"<td>" + data[i].order_username + "</td>"+
"<td>" + data[i].work_description + "</td>" +
"<td>" + dateFormatter(data[i].orderdate) + "</td>" +
"<td>" + dateFormatter(data[i].startdate) + "</td>" +
"<td>" + dateFormatter(data[i].readydate) + "</td>" +
"<td>" + dateFormatter(data[i].accepteddate) + "</td>" +
"<td>" + dateFormatter(data[i].denieddate) + "</td>" +
"<td>" + data[i].comment_of_work + "</td>" +
"<td>" + data[i].hours + "</td>" +
"<td>" + data[i].approx_budget + "</td>" +
"<td>" + data[i].status + "</td>" +
button +
"</tr>"
)
alert(data[i].id);
$(".delete").click(() => {
deleteUser(data[i].id);
})
}
}
function deleteUser(key) {
$.ajax(
{
url: "http:localhost:3001/workorders_delete/"+key,
method: 'delete'
}).done( (data, status, jqXHR) => {
}).fail( (jqXHR, status, errorThrown) => {
console.log("Call failed: "+errorThrown);
});
}
javascript jquery
add a comment |
I want to add delete button to every row where status is ORDERED. I have tried different variations. This solution could work without error "Cannot read property 'id' of undefined" even though data[i] is not undefined. Alert before .click() gives right id. What is wrong here? Can i do something differently or better here?
).done( (data, status, jqXHR) => {
if(data[0].order_username != undefined) {
$("#orders_table").empty();
for(var i = 0; i < data.length; i++) {
var button = "";
if(data[i].status == "ORDERED") {
button = "<td><button class='delete'>Delete</button></td>";
}
$("#orders_table").append(
"<tr>" +
"<td>" + data[i].order_username + "</td>"+
"<td>" + data[i].work_description + "</td>" +
"<td>" + dateFormatter(data[i].orderdate) + "</td>" +
"<td>" + dateFormatter(data[i].startdate) + "</td>" +
"<td>" + dateFormatter(data[i].readydate) + "</td>" +
"<td>" + dateFormatter(data[i].accepteddate) + "</td>" +
"<td>" + dateFormatter(data[i].denieddate) + "</td>" +
"<td>" + data[i].comment_of_work + "</td>" +
"<td>" + data[i].hours + "</td>" +
"<td>" + data[i].approx_budget + "</td>" +
"<td>" + data[i].status + "</td>" +
button +
"</tr>"
)
alert(data[i].id);
$(".delete").click(() => {
deleteUser(data[i].id);
})
}
}
function deleteUser(key) {
$.ajax(
{
url: "http:localhost:3001/workorders_delete/"+key,
method: 'delete'
}).done( (data, status, jqXHR) => {
}).fail( (jqXHR, status, errorThrown) => {
console.log("Call failed: "+errorThrown);
});
}
javascript jquery
I want to add delete button to every row where status is ORDERED. I have tried different variations. This solution could work without error "Cannot read property 'id' of undefined" even though data[i] is not undefined. Alert before .click() gives right id. What is wrong here? Can i do something differently or better here?
).done( (data, status, jqXHR) => {
if(data[0].order_username != undefined) {
$("#orders_table").empty();
for(var i = 0; i < data.length; i++) {
var button = "";
if(data[i].status == "ORDERED") {
button = "<td><button class='delete'>Delete</button></td>";
}
$("#orders_table").append(
"<tr>" +
"<td>" + data[i].order_username + "</td>"+
"<td>" + data[i].work_description + "</td>" +
"<td>" + dateFormatter(data[i].orderdate) + "</td>" +
"<td>" + dateFormatter(data[i].startdate) + "</td>" +
"<td>" + dateFormatter(data[i].readydate) + "</td>" +
"<td>" + dateFormatter(data[i].accepteddate) + "</td>" +
"<td>" + dateFormatter(data[i].denieddate) + "</td>" +
"<td>" + data[i].comment_of_work + "</td>" +
"<td>" + data[i].hours + "</td>" +
"<td>" + data[i].approx_budget + "</td>" +
"<td>" + data[i].status + "</td>" +
button +
"</tr>"
)
alert(data[i].id);
$(".delete").click(() => {
deleteUser(data[i].id);
})
}
}
function deleteUser(key) {
$.ajax(
{
url: "http:localhost:3001/workorders_delete/"+key,
method: 'delete'
}).done( (data, status, jqXHR) => {
}).fail( (jqXHR, status, errorThrown) => {
console.log("Call failed: "+errorThrown);
});
}
javascript jquery
javascript jquery
asked Nov 17 '18 at 14:30
A.PartanenA.Partanen
111
111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can set unique data attributes to the delete buttons using the the ids from the response data as follows:-
if(data[i].status == "ORDERED") {
button = "<td><button class='delete' data-deleteId =
"+data[i].id+">Delete</button></td>";
}
and then you can write a single click event listener for the delete buttons as follows:-
$(".delete").click(function(){
var id = $(this).data("deleteId");
deleteUser(id);
});
In your code :-
$(".delete").click(() => {
deleteUser(data[i].id);
});
the function deleteUser() is being called or executed on click and at the point data[i].id will not be defined.
There is no explicitthis
in arrow functions
– charlietfl
Nov 17 '18 at 15:22
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
add a comment |
Why is data[i]
undefined
?
In the loop, i
is a reference to the same variable that has the value data.length
after the loop.
As expected, data[data.length]
is undefined
. Check this with alert(i)
inside .click()
.
$(".delete").last().click(() => {
alert(i);
deleteUser(data[i].id);
})
A simplified example:
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
The solution is to create a new const
in the loop:
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
In addition...
$(".delete")
refers to all delete buttons that exist at the point you register .click()
.
You should register only on the last delete button:
const constI = i;
$(".delete").last().click(() => {
deleteUser(data[constI].id);
})
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%2f53352167%2fadding-button-with-onclick-event-in-append-with-jquery-javascipt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can set unique data attributes to the delete buttons using the the ids from the response data as follows:-
if(data[i].status == "ORDERED") {
button = "<td><button class='delete' data-deleteId =
"+data[i].id+">Delete</button></td>";
}
and then you can write a single click event listener for the delete buttons as follows:-
$(".delete").click(function(){
var id = $(this).data("deleteId");
deleteUser(id);
});
In your code :-
$(".delete").click(() => {
deleteUser(data[i].id);
});
the function deleteUser() is being called or executed on click and at the point data[i].id will not be defined.
There is no explicitthis
in arrow functions
– charlietfl
Nov 17 '18 at 15:22
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
add a comment |
You can set unique data attributes to the delete buttons using the the ids from the response data as follows:-
if(data[i].status == "ORDERED") {
button = "<td><button class='delete' data-deleteId =
"+data[i].id+">Delete</button></td>";
}
and then you can write a single click event listener for the delete buttons as follows:-
$(".delete").click(function(){
var id = $(this).data("deleteId");
deleteUser(id);
});
In your code :-
$(".delete").click(() => {
deleteUser(data[i].id);
});
the function deleteUser() is being called or executed on click and at the point data[i].id will not be defined.
There is no explicitthis
in arrow functions
– charlietfl
Nov 17 '18 at 15:22
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
add a comment |
You can set unique data attributes to the delete buttons using the the ids from the response data as follows:-
if(data[i].status == "ORDERED") {
button = "<td><button class='delete' data-deleteId =
"+data[i].id+">Delete</button></td>";
}
and then you can write a single click event listener for the delete buttons as follows:-
$(".delete").click(function(){
var id = $(this).data("deleteId");
deleteUser(id);
});
In your code :-
$(".delete").click(() => {
deleteUser(data[i].id);
});
the function deleteUser() is being called or executed on click and at the point data[i].id will not be defined.
You can set unique data attributes to the delete buttons using the the ids from the response data as follows:-
if(data[i].status == "ORDERED") {
button = "<td><button class='delete' data-deleteId =
"+data[i].id+">Delete</button></td>";
}
and then you can write a single click event listener for the delete buttons as follows:-
$(".delete").click(function(){
var id = $(this).data("deleteId");
deleteUser(id);
});
In your code :-
$(".delete").click(() => {
deleteUser(data[i].id);
});
the function deleteUser() is being called or executed on click and at the point data[i].id will not be defined.
edited Nov 17 '18 at 15:34
answered Nov 17 '18 at 14:59
ssamuelssamuel
2646
2646
There is no explicitthis
in arrow functions
– charlietfl
Nov 17 '18 at 15:22
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
add a comment |
There is no explicitthis
in arrow functions
– charlietfl
Nov 17 '18 at 15:22
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
There is no explicit
this
in arrow functions– charlietfl
Nov 17 '18 at 15:22
There is no explicit
this
in arrow functions– charlietfl
Nov 17 '18 at 15:22
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks.. Noted..
– ssamuel
Nov 17 '18 at 15:34
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
Thanks, this worked
– A.Partanen
Nov 17 '18 at 17:01
add a comment |
Why is data[i]
undefined
?
In the loop, i
is a reference to the same variable that has the value data.length
after the loop.
As expected, data[data.length]
is undefined
. Check this with alert(i)
inside .click()
.
$(".delete").last().click(() => {
alert(i);
deleteUser(data[i].id);
})
A simplified example:
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
The solution is to create a new const
in the loop:
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
In addition...
$(".delete")
refers to all delete buttons that exist at the point you register .click()
.
You should register only on the last delete button:
const constI = i;
$(".delete").last().click(() => {
deleteUser(data[constI].id);
})
add a comment |
Why is data[i]
undefined
?
In the loop, i
is a reference to the same variable that has the value data.length
after the loop.
As expected, data[data.length]
is undefined
. Check this with alert(i)
inside .click()
.
$(".delete").last().click(() => {
alert(i);
deleteUser(data[i].id);
})
A simplified example:
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
The solution is to create a new const
in the loop:
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
In addition...
$(".delete")
refers to all delete buttons that exist at the point you register .click()
.
You should register only on the last delete button:
const constI = i;
$(".delete").last().click(() => {
deleteUser(data[constI].id);
})
add a comment |
Why is data[i]
undefined
?
In the loop, i
is a reference to the same variable that has the value data.length
after the loop.
As expected, data[data.length]
is undefined
. Check this with alert(i)
inside .click()
.
$(".delete").last().click(() => {
alert(i);
deleteUser(data[i].id);
})
A simplified example:
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
The solution is to create a new const
in the loop:
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
In addition...
$(".delete")
refers to all delete buttons that exist at the point you register .click()
.
You should register only on the last delete button:
const constI = i;
$(".delete").last().click(() => {
deleteUser(data[constI].id);
})
Why is data[i]
undefined
?
In the loop, i
is a reference to the same variable that has the value data.length
after the loop.
As expected, data[data.length]
is undefined
. Check this with alert(i)
inside .click()
.
$(".delete").last().click(() => {
alert(i);
deleteUser(data[i].id);
})
A simplified example:
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
The solution is to create a new const
in the loop:
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
In addition...
$(".delete")
refers to all delete buttons that exist at the point you register .click()
.
You should register only on the last delete button:
const constI = i;
$(".delete").last().click(() => {
deleteUser(data[constI].id);
})
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
(function wrong() {
var array = ;
for (i = 0; i < 2; i++) {
array.push(() => {
console.log(i);
});
}
array.forEach(click => click());
})();
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
(function right() {
var array = ;
for (i = 0; i < 2; i++) {
const constI = i;
array.push(() => {
console.log(constI);
});
}
array.forEach(click => click());
})();
edited Nov 17 '18 at 15:31
answered Nov 17 '18 at 15:07
aaronaaron
8,64131131
8,64131131
add a comment |
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%2f53352167%2fadding-button-with-onclick-event-in-append-with-jquery-javascipt%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