I have hacked together this jQuery script and it is all working fine except that the alert is being fired when it shouldn’t be, and I can’t work out why.
jQuery(document).ready(function(){
function countDouble() {
var d = $("input.double:checked").length;
var s = $("input.single:checked").length;
if (s === 1 && d === 2) {
$("a#proj-btn").attr("href", "#tab2");
} else {
$("a#proj-btn").attr("href", "http://www.someurl.com.au/new-cruise-name-home");
$("#proj-btn").click( function() {
alert('Please select which projects you are interested in before continuing with enquiry.\nSelect 1 x 1 day project and 2 x 2 day projects by ticking the box beneath your chosen projects.');
});
};
}
countDouble();
$(":checkbox").click(countDouble);
});
Here is the HTML
<ul class="tabs">
<li class="right"><a id="proj-btn" href="#">NEXT >> Cabin Choice</a></li>
</ul>
So when there is 1 x .single checkbox checked and 2 x .double checkboxes checked, it adds the URL of #tab2 to the button which works fine.
But it also displays the alert 3 times after clicking the button when the checkboxes are checked. The alert should only be when the checkboxes are not checked.
What have I done wrong? I can’t work this out
Try Unbinding ‘click’ every time you call
countDouble()and pullingcountDouble()outside of the ready function (probably not necessary).