I want to make it so that when the compare button is pressed, and the table is shown, the button changes to "back to products" and it takes the user back to products.php when clicked.
Currently if products are being compared, and the user presses the button again, the table just adds the products again doubling the number of rows.
<body>
<div id="content">
<div class="bg-img-container" style="margin-top: -1.5%;">
<img src="images/books-bg.jpg" alt="Book Image" style="width:100%;">
</div>
<br><br>
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12" style="text-align: center;">
<script>
var count = 0; //for counting clicks
$(function(){
$(".product").on('click', function(e) {
e.preventDefault();
//if class is already there, decrement to remove book selection
if ($(this).closest('.product').hasClass("compare")) {
$(this).closest('.product').removeClass("compare");
count = count - 1;
} else if (count <= 2) {
count += 1; //increment count
$(this).closest('.product').toggleClass('compare');
} else { //cannot select more than 3
alert("Please Limit your Selection to 3 books");
}
});
});
function buildComparisonTable(){
var comparisonTableBody = $('table.comparison tbody');
var comparisonTableBodyProductTitleCol = $('table.comparison thead tr.product-title');
var comparisonTableBodyProductImgCol = $('table.comparison tbody tr.product-img');
var comparisonTableBodyProductScopeCol = $('table.comparison tbody tr.product-scope');
var comparisonTableBodyProductImpactCol = $('table.comparison tbody tr.product-impact');
var comparisonTableBodyProductorcidCol = $('table.comparison tbody tr.product-orcid');
var comparisonTableBodyProductoaCol = $('table.comparison tbody tr.product-oa');
var comparisonTableBodyProductdspCol = $('table.comparison tbody tr.product-dsp');
comparisonTableBody.find('.product-col').remove();
$('.product.compare').each(function(){
var id = $(this).attr('data-id');
var title = $(this).attr('data-title');
var img = $(this).attr('data-img');
var scope = $(this).attr('data-scope');
var impact = $(this).attr('data-impact');
var orcid = $(this).attr('data-orcid');
var oa = $(this).attr('data-oa');
var dsp = $(this).attr('data-dsp');
comparisonTableBodyProductTitleCol.append('<th class="product-col"><center>'+title+'</center></th>');
comparisonTableBodyProductImgCol.append('<td class="product-col"><img src='+img+'></td>');
comparisonTableBodyProductScopeCol.append('<td class="product-col">'+scope+'</td>');
comparisonTableBodyProductImpactCol.append('<td class="product-col">'+impact+'</td>');
comparisonTableBodyProductorcidCol.append('<td class="product-col">'+orcid+'</td>');
comparisonTableBodyProductoaCol.append('<td class="product-col">'+oa+'</td>');
comparisonTableBodyProductdspCol.append('<td class="product-col">'+dsp+'</td>');
});
}
</script>
</div>
<div class="col-md-12 col-sm-8">
<hr>
<h4 style="float: left; text-align: left;">Select up to 3 books, Click "Compare Titles", View your Top Picks Side-by-Side!</h4>
<button class='btn goCompare' id="compare-remove"; style="color: white">Compare Titles</button>
<table class='comparison' border="1px" style="float: left;">
<thead>
<tr class='product-title'></tr>
</thead>
<tbody>
<tr class='product-img'></tr>
<tr class='product-impact'></tr>
<tr class='product-oa'></tr>
<tr class='product-scope'></tr>
<tr class='product-orcid'></tr>
<tr class='product-dsp'></tr>
</tbody>
</table>
</div>
<script>
function removeSelection() {
var x = document.getElementById("compare-remove");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
</div>
</div>
</div>
alink styled as a button.