I am creating a flight booking system. Once the user queries for flights based on some criteria, I need to display the data returned from the server in a table with a 'Book Now' button on each row to book that particular flight.
I am using JavaScript to populate the table. My problem is that I am unable to dynamically generate buttons with custom onclick. In onclick, I want to call functions with multiple parameters. But somehow, my code is not working.
This is the JavaScript string I am appending to my table to generate a table cell with the custom button -
"<td><button id = 'bookButton' onclick=bookFlight('" + data['flight_no'] + "', '" + data['departure_destination'] + "', '" + data["arrival_destination"] + "', '" + data["seat_class"] + "', '" + data['departure_details'] + "') value='Book Now'></button></td>"
When I console log this string, the output is actually what I want. For example -
<td>JT216</td><td>Singapore</td><td>Bangkok</td><td>2013-12-01 18:00:00</td><td>2013-12-01 19:30:00</td><td>JetStar Air</td><td>B787</td><td>$100</td><td><button id = 'bookButton' onclick=bookFlight('JT216', 'Singapore', 'Bangkok', 'Economy', '2013-12-01 18:00:00') value='Book Now'></button></td>
However, it doesn't get added to the DOM correctly. This is what the cell looks like in the page source -
<button id="bookButton" onclick="bookFlight('JT216'," 'singapore',="" 'bangkok',="" 'economy',="" '2013-12-01="" 18:00:00')="" value="Book Now"></button>
Could someone please tell me why this happens?
I have stripped down my code to the bare basics so that it can help you to resolve my problem. This is the HTML file you can use to replicate and see the problem yourself.
<html>
<head>
<title>
Flight Reservation System
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/custom.css">
</head>
<body>
<div class = "container">
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-fields">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Fly<em>Smart</em></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-fields">
<ul class="nav navbar-nav navbar-right">
<li><a href="/flightSearchPage.php">Search For Flights</a></li>
<li><a href="/flightSearchPage.php">Manage Booking</a></li>
<li><a href="/flightSearchPage.php">Admin Log In</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
<div id = "outerContainer">
<div id = "innerContainer" class = "row-fluid">
<form id = "searchForm" class = "text-center searchPos">
<fieldset>
<br/>
<div class = "row-fluid">
<label>Departure City: </label>
<select id = "depCities" name = "fromCity"></select>
<label> Arrival City: </label>
<select id = "arrCities" name = "toCity"></select>
<label>Seat Class: </label>
<select id = "seatClass" name = "class">
</select>
<label>Date of Departure:</label>
<input name = "departureDate" id = "depDate" type = "date"></input>
<strong>1</strong><input name = "numPeople" id = "numPeople" type = "range"
min = "1" max = "150" default = "1" onchange = "updateRangeValue();"></input><strong>150</strong>
<br/><strong>Selected Seats: <output id="numSelected"> </output></strong>
<br/><br/>
<input class = "btn btn-primary" type = "submit" value = "Search for Flights"></input>
</div>
</fieldset>
</form>
<br/>
<table id = "resultsTable" class = "table">
</table>
<br/>
<br/>
<p id = "Log"></p>
</div>
</div>
<script type="text/javascript">
$("#resultsTable thead").remove();
$("#resultsTable tbody").remove();
$("#resultsTable").append("<thead><tr>" + printTableHead() +"</tr></thead><tbody>");
tableData = [{0: "JT216", 1: "JetStar Air", 2: "Singapore",
3: "Bangkok",
4: "2013-12-01 18:00:00",
5: "2013-12-01 19:30:00",
6: "80",
7: "B787",
8: "Economy",
9: "80",
10: "100",
11: "JT216",
12: "2013-12-01 18:00:00",
arrival_destination: "Bangkok",
arrival_details: "2013-12-01 19:30:00",
available_seats: "80",
capacity: "80",
carrier_name: "JetStar Air",
departure_destination: "Singapore",
departure_details: "2013-12-01 18:00:00",
flight_no: "JT216",
plane_model_no: "B787",
price: "100",
seat_class: "Economy"}];
$.each(tableData, function() {
$("#resultsTable").append("<tr>" + printTupleData(this) +"</tr>");
console.log("<tr>" + printTupleData(this) +"</tr>");
});
$("#resultsTable").append("</tbody");
$("#resultsTable").append("</tbody");
function printTableHead() {
tuple = "";
tuple += ("<th>Flight Number</th>");
tuple += ("<th>From</th>");
tuple += ("<th>To</th>");
tuple += ("<th>Departure Date/Time</th>");
tuple += ("<th>Arrival Date/Time</th>");
tuple += ("<th>Airline</th>");
tuple += ("<th>Aircraft Model</th>");
tuple += ("<th>Price</th>");
tuple += ("<th>Book Option</th>");
return tuple;
};
function printTupleData(data) {
tuple = "";
tuple += ("<td>" + data["flight_no"] + "</td>");
tuple += ("<td>" + data["departure_destination"] + "</td>");
tuple += ("<td>" + data["arrival_destination"] + "</td>");
tuple += ("<td>" + data["departure_details"] + "</td>");
tuple += ("<td>" + data["arrival_details"] + "</td>");
tuple += ("<td>" + data["carrier_name"] + "</td>");
tuple += ("<td>" + data["plane_model_no"] + "</td>");
tuple += ("<td>$" + data["price"] + "</td>");
tuple += ("<td><button id = 'bookButton' onclick=bookFlight('" + data['flight_no'] + "', '" + data['departure_destination'] + "', '" + data["arrival_destination"] + "', '" + data["seat_class"] + "', '" + data['departure_details'] + "') value='Book Now'></button></td>");
console.log(tuple);
// tuple += ("<td><form id = 'bookForm'><fieldset><input value = 'Book Now' type = 'submit' class = 'btn btn-primary' id = 'bookButton'/></fieldset></form></td>");
//console.log(data["departure_details"]);
// flight ="
// + data["flight_no"] + " departureTime ='"+ data["departure_details"] +
// "' seatClass = "+ data["seat_class"] + "
//
return tuple;
};
</script>
</body>
</html>