In the following html, javascript, bootstrap form the click event handler works properly and adds a new dynamic row but the remove button does not alert out anything when clicked on it. There are no error messages on the debugger console so i am not sure what's wrong.
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title> Reggie Scenarios </title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<!-- #CSS Links -->
<!-- Basic Styles -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Credits: http://bootsnipp.com/snippets/featured/responsive-navigation-menu
-->
<link rel="stylesheet" type="text/css" href="leftMenu.css">
<script src="scenario.js"></script>
</head>
<body>
<div class="mainForm">
<div class="container">
<h2>Scenario</h2>
<form id="bookForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-xs-1 control-label">Book</label>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0].title" placeholder="Title" />
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0].isbn" placeholder="ISBN" />
</div>
<div class="col-xs-2">
<input type="text" class="form-control" name="book[0].price" placeholder="Price" />
</div>
<div class="col-xs-1">
<button id= "plusButton1" type="button" class="btn btn-default addButton"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
<div class="form-group hide" id="bookTemplate">
<div class="col-xs-4 col-xs-offset-1">
<input type="text" class="form-control" name="title" placeholder="Title" />
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="isbn" placeholder="ISBN" />
</div>
<div class="col-xs-2">
<input type="text" class="form-control" name="price" placeholder="Price" />
</div>
<div class="col-xs-1">
<button id = "removeButton" type="button" class="btn btn-default removeButton"><span class="glyphicon glyphicon-minus"></span></button>
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-1">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</div>
</body>
<script>
bookIndex = 0;
$(document).ready(function() {
$(".removeButton").click(function() {
alert("Remove button was clicked!");
var id = $(this).attr("id");
alert(id);
var $row = $(this).parents('.form-group'),
index = $row.attr('data-book-index');
// Remove element containing the fields
$row.remove();
});
$(".addButton" ).click(function(event) {
alert("Add button was clicked!");
bookIndex++;
var $template = $('#bookTemplate'),
$clone = $template
.clone()
.removeClass('hide')
.removeAttr('id')
.attr('data-book-index', bookIndex)
.insertBefore($template);
$clone
.find('[name="title"]').attr('name', 'book[' + bookIndex + '].title').end()
.find('[name="isbn"]').attr('name', 'book[' + bookIndex + '].isbn').end()
.find('[name="price"]').attr('name', 'book[' + bookIndex + '].price').end();
});
});
Please advise on what i may be doing wrong. I have created a jsfiddle here: https://jsfiddle.net/snehilw/7Luxo0L4/
In the fiddle, the add button works, but the remove button won't work. I do not get any errors in the debugger console.
Please advise,
thanks!