I have an add-row
button. On its click I am adding a row into my datatable
.
JavaScript
x
26
26
1
$('.add-row').on('click', function() {
2
id = 0;
3
name = $('#name').val();
4
email = $('#email').val();
5
cno = $('#cno').val();
6
bdy = $('#bday').val();
7
8
t.row.add([
9
name,
10
email,
11
cno,
12
bdy,
13
'<button class ="btn btn-danger del">Delete</button>' + '' + '<button class="btn btn-warning upd" data-id="'+$('#example').DataTable().rows().count()+'">Edit</button>'
14
15
16
17
18
19
]).draw(false);
20
$("#name").val('');
21
$("#email").val('');
22
$("#cno").val('');
23
$("#bday").val('');
24
25
});
26
In the above code, I am using data-id
and adding row id
to it. On my edit
button click I am getting the data-id
value.
JavaScript
1
28
28
1
$("body").on("click", ".upd", function(e) {
2
const id = $(this).attr("data-id");
3
console.log(id);
4
// Prevent event propagation
5
e.stopPropagation();
6
var $row = $(this).closest('tr');
7
var data = t.row($row).data();
8
9
//alert('Edit ' + data[0]);
10
name = data[0];
11
email = data[1];
12
cno = data[2];
13
bdy = data[3];
14
15
console.log(name);
16
console.log(email);
17
console.log(cno);
18
console.log(bdy);
19
20
$("#name1").val(name);
21
$("#email1").val(email);
22
$("#cno1").val(cno);
23
$("#dob1").val(bdy);
24
$("#myModal").modal('show');
25
26
27
});
28
The above code takes input data from modal
. In my modal
I have a save
button having id btnsubmit
. Now on this button click, I want to pass my data-id
.
JavaScript
1
14
14
1
$("#btnsubmit").on('click',function () {//in this event i want to pass the `data-id`
2
3
4
var new_name = $("#name1").val();
5
var new_email = $("#email1").val();
6
var new_cno = $("#cno1").val();
7
var new_dob = $("#dob1").val();
8
9
10
11
12
$("#myModal").modal("hide");
13
});
14
How can I pass the value of data-id
in it?
Any help would be highly appreciated.
Advertisement
Answer
you could stock your data in html modal like this:
JavaScript
1
2
1
$("#myModal").data('id', id);
2
and use:
JavaScript
1
2
1
var id = $("#myModal").data('id');
2