Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use JavaScript to put a button with an event listener in HTML table
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>This is the JavaScript string I am appending to my table to generate a table cell with the custom button -</p> <pre><code>"&lt;td&gt;&lt;button id = 'bookButton' onclick=bookFlight('" + data['flight_no'] + "', '" + data['departure_destination'] + "', '" + data["arrival_destination"] + "', '" + data["seat_class"] + "', '" + data['departure_details'] + "') value='Book Now'&gt;&lt;/button&gt;&lt;/td&gt;" </code></pre> <p>When I console log this string, the output is actually what I want. For example -</p> <pre><code>&lt;td&gt;JT216&lt;/td&gt;&lt;td&gt;Singapore&lt;/td&gt;&lt;td&gt;Bangkok&lt;/td&gt;&lt;td&gt;2013-12-01 18:00:00&lt;/td&gt;&lt;td&gt;2013-12-01 19:30:00&lt;/td&gt;&lt;td&gt;JetStar Air&lt;/td&gt;&lt;td&gt;B787&lt;/td&gt;&lt;td&gt;$100&lt;/td&gt;&lt;td&gt;&lt;button id = 'bookButton' onclick=bookFlight('JT216', 'Singapore', 'Bangkok', 'Economy', '2013-12-01 18:00:00') value='Book Now'&gt;&lt;/button&gt;&lt;/td&gt; </code></pre> <p>However, it doesn't get added to the DOM correctly. This is what the cell looks like in the page source -</p> <pre><code>&lt;button id="bookButton" onclick="bookFlight('JT216'," 'singapore',="" 'bangkok',="" 'economy',="" '2013-12-01="" 18:00:00')="" value="Book Now"&gt;&lt;/button&gt; </code></pre> <p>Could someone please tell me why this happens?</p> <p>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.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt; Flight Reservation System &lt;/title&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;script src="js/jquery-1.10.2.min.js"&gt;&lt;/script&gt; &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="css/bootstrap.min.css"&gt; &lt;link rel="stylesheet" href="css/custom.css"&gt; &lt;/head&gt; &lt;body&gt; &lt;div class = "container"&gt; &lt;nav class="navbar navbar-default" role="navigation"&gt; &lt;!-- Brand and toggle get grouped for better mobile display --&gt; &lt;div class="navbar-header"&gt; &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-fields"&gt; &lt;span class="sr-only"&gt;Toggle navigation&lt;/span&gt; &lt;span class="icon-bar"&gt;&lt;/span&gt; &lt;span class="icon-bar"&gt;&lt;/span&gt; &lt;span class="icon-bar"&gt;&lt;/span&gt; &lt;/button&gt; &lt;a class="navbar-brand" href="#"&gt;Fly&lt;em&gt;Smart&lt;/em&gt;&lt;/a&gt; &lt;/div&gt; &lt;!-- Collect the nav links, forms, and other content for toggling --&gt; &lt;div class="collapse navbar-collapse" id="navbar-fields"&gt; &lt;ul class="nav navbar-nav navbar-right"&gt; &lt;li&gt;&lt;a href="/flightSearchPage.php"&gt;Search For Flights&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/flightSearchPage.php"&gt;Manage Booking&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/flightSearchPage.php"&gt;Admin Log In&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;&lt;!-- /.navbar-collapse --&gt; &lt;/nav&gt; &lt;div id = "outerContainer"&gt; &lt;div id = "innerContainer" class = "row-fluid"&gt; &lt;form id = "searchForm" class = "text-center searchPos"&gt; &lt;fieldset&gt; &lt;br/&gt; &lt;div class = "row-fluid"&gt; &lt;label&gt;Departure City: &lt;/label&gt; &lt;select id = "depCities" name = "fromCity"&gt;&lt;/select&gt; &lt;label&gt; Arrival City: &lt;/label&gt; &lt;select id = "arrCities" name = "toCity"&gt;&lt;/select&gt; &lt;label&gt;Seat Class: &lt;/label&gt; &lt;select id = "seatClass" name = "class"&gt; &lt;/select&gt; &lt;label&gt;Date of Departure:&lt;/label&gt; &lt;input name = "departureDate" id = "depDate" type = "date"&gt;&lt;/input&gt; &lt;strong&gt;1&lt;/strong&gt;&lt;input name = "numPeople" id = "numPeople" type = "range" min = "1" max = "150" default = "1" onchange = "updateRangeValue();"&gt;&lt;/input&gt;&lt;strong&gt;150&lt;/strong&gt; &lt;br/&gt;&lt;strong&gt;Selected Seats: &lt;output id="numSelected"&gt;&amp;nbsp;&lt;/output&gt;&lt;/strong&gt; &lt;br/&gt;&lt;br/&gt; &lt;input class = "btn btn-primary" type = "submit" value = "Search for Flights"&gt;&lt;/input&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;br/&gt; &lt;table id = "resultsTable" class = "table"&gt; &lt;/table&gt; &lt;br/&gt; &lt;br/&gt; &lt;p id = "Log"&gt;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $("#resultsTable thead").remove(); $("#resultsTable tbody").remove(); $("#resultsTable").append("&lt;thead&gt;&lt;tr&gt;" + printTableHead() +"&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;"); 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("&lt;tr&gt;" + printTupleData(this) +"&lt;/tr&gt;"); console.log("&lt;tr&gt;" + printTupleData(this) +"&lt;/tr&gt;"); }); $("#resultsTable").append("&lt;/tbody"); $("#resultsTable").append("&lt;/tbody"); function printTableHead() { tuple = ""; tuple += ("&lt;th&gt;Flight Number&lt;/th&gt;"); tuple += ("&lt;th&gt;From&lt;/th&gt;"); tuple += ("&lt;th&gt;To&lt;/th&gt;"); tuple += ("&lt;th&gt;Departure Date/Time&lt;/th&gt;"); tuple += ("&lt;th&gt;Arrival Date/Time&lt;/th&gt;"); tuple += ("&lt;th&gt;Airline&lt;/th&gt;"); tuple += ("&lt;th&gt;Aircraft Model&lt;/th&gt;"); tuple += ("&lt;th&gt;Price&lt;/th&gt;"); tuple += ("&lt;th&gt;Book Option&lt;/th&gt;"); return tuple; }; function printTupleData(data) { tuple = ""; tuple += ("&lt;td&gt;" + data["flight_no"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;" + data["departure_destination"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;" + data["arrival_destination"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;" + data["departure_details"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;" + data["arrival_details"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;" + data["carrier_name"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;" + data["plane_model_no"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;$" + data["price"] + "&lt;/td&gt;"); tuple += ("&lt;td&gt;&lt;button id = 'bookButton' onclick=bookFlight('" + data['flight_no'] + "', '" + data['departure_destination'] + "', '" + data["arrival_destination"] + "', '" + data["seat_class"] + "', '" + data['departure_details'] + "') value='Book Now'&gt;&lt;/button&gt;&lt;/td&gt;"); console.log(tuple); // tuple += ("&lt;td&gt;&lt;form id = 'bookForm'&gt;&lt;fieldset&gt;&lt;input value = 'Book Now' type = 'submit' class = 'btn btn-primary' id = 'bookButton'/&gt;&lt;/fieldset&gt;&lt;/form&gt;&lt;/td&gt;"); //console.log(data["departure_details"]); // flight =" // + data["flight_no"] + " departureTime ='"+ data["departure_details"] + // "' seatClass = "+ data["seat_class"] + " // return tuple; }; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload