I am trying to pass the reservation id from HTML to modal. In pictures, when I click “cancel” next to reservation: preview of the screen A modal appears and it should contain the id number of reservation: preview
Modal pops up but without the reservation id number. Please, what’s wrong?
I followed this tutorial: https://www.geeksforgeeks.org/how-to-pass-data-into-a-bootstrap-modal/. Thank you.
Here is my code:
JavaScript
x
87
87
1
{% extends "layout.html" %}
2
3
{% block title %}
4
Index
5
{% endblock %}
6
7
{% block main %}
8
<p>
9
<h3>Welcome {{ firstname }}</h3>
10
</p>
11
12
<table class="table table-striped">
13
<thead>
14
<tr>
15
<th>Seat</th>
16
<th>Start date</th>
17
<th>End date</th>
18
<th>Number of days</th>
19
<th>Action</th>
20
21
</tr>
22
</thead>
23
24
<tbody>
25
26
{% for histor in history %}
27
28
<tr>
29
<td>{{ histor.seat_name }}</td>
30
<td>{{ histor.start_date}}</td>
31
<td>{{ histor.end_date }}</td>
32
<td>{{ numberofdays }}</td>
33
<td>
34
<form action="/" method="post">
35
<input tupe="text" class="form-control" id="idtocancel" name="idtocancel" autocomplete="on" selected placeholder={{ histor.booking_id }}>
36
<button type="button" id="submit" class="btn btn-success tocancel" data-toggle="modal" data-target="#exampleModal">Cancel {{ histor.booking_id}}</button>
37
</form>
38
39
</td>
40
</tr>
41
{% endfor %}
42
43
</tbody>
44
45
</table>
46
47
<!-- Modal -->
48
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
49
<div class="modal-dialog" role="document">
50
<div class="modal-content">
51
<div class="modal-header">
52
<h5 class="modal-title" id="exampleModalLabel">Do you really wish to cancel this booking?</h5>
53
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
54
<span aria-hidden="true">×</span>
55
</button>
56
</div>
57
<div class="modal-body">
58
<h6 id="modal_body"></h6>
59
60
</div>
61
<div class="modal-footer">
62
63
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, go back</button>
64
65
<input class="form-control" name="bookId" id="bookId" autocomplete="on" selected>
66
<button type="button" id="submit" class="btn btn-success" data-toggle="modal" data-target="#exampleModal">Yes, cancel the booking</button>
67
</div>
68
</div>
69
</div>
70
</div>
71
72
<!--JavaScript queries-->
73
74
75
<script type="text/javascript">
76
77
$("#submit").click(function () {
78
var name = $("#idtocancel").val();
79
$("#modal_body").html( name);
80
});
81
82
</script>
83
84
85
86
{% endblock %}
87
Advertisement
Answer
You are assigning value of {{ histor.booking_id }}
to placeholder instead use value="{{ histor.booking_id }}"
.Then , use class for click event and inside this get value of input using $(this).prev().val()
and put it inside your modal.
Demo Code :
JavaScript
1
5
1
$(".tocancel").click(function() {
2
var name = $(this).prev().val(); //use prev
3
$("#modal_body").html(name);
4
$("#bookId").val(name); //use val here
5
});
JavaScript
1
73
73
1
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
2
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
3
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
4
5
<table class="table table-striped">
6
<thead>
7
<tr>
8
<th>Seat</th>
9
<th>Start date</th>
10
<th>End date</th>
11
<th>Number of days</th>
12
<th>Action</th>
13
14
</tr>
15
</thead>
16
17
<tbody>
18
19
<tr>
20
<td>A</td>
21
<td>2-10-1</td>
22
<td>10-1-10</td>
23
<td>5</td>
24
<td>
25
<form action="/" method="post">
26
<!--added value="{{ histor.booking_id }}"-->
27
<input type="text" class="form-control" name="idtocancel" autocomplete="on" selected placeholder={{ histor.booking_id }} value="1">
28
<button type="button" class="btn btn-success tocancel" data-toggle="modal" data-target="#exampleModal">Cancel 1</button>
29
</form>
30
31
</td>
32
</tr>
33
<tr>
34
<td>B</td>
35
<td>2-10-1</td>
36
<td>10-1-11</td>
37
<td>5</td>
38
<td>
39
<form action="/" method="post">
40
<input type="text" class="form-control" name="idtocancel" autocomplete="on" selected placeholder={{ histor.booking_id }} value="2">
41
<button type="button" class="btn btn-success tocancel" data-toggle="modal" data-target="#exampleModal">Cancel 2</button>
42
</form>
43
44
</td>
45
</tr>
46
</tbody>
47
48
</table>
49
50
<!-- Modal -->
51
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
52
<div class="modal-dialog" role="document">
53
<div class="modal-content">
54
<div class="modal-header">
55
<h5 class="modal-title" id="exampleModalLabel">Do you really wish to cancel this booking?</h5>
56
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
57
<span aria-hidden="true">×</span>
58
</button>
59
</div>
60
<div class="modal-body">
61
<h6 id="modal_body"></h6>
62
63
</div>
64
<div class="modal-footer">
65
66
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, go back</button>
67
68
<input class="form-control" name="bookId" id="bookId" autocomplete="on" selected>
69
<button type="button" id="submit" class="btn btn-success" data-toggle="modal" data-target="#exampleModal">Yes, cancel the booking</button>
70
</div>
71
</div>
72
</div>
73
</div>