Reputation: 103
I am trying to push the values of the clicked buttons into a JS array named values
. After that I want to check the length of of this array and if it has the length of 6 the condition is true. The problem with this code is that I get no alert.
JS
var values = [];
jQuery(document).ready(function($){
$('.ui-btn').click(function() {
values.push($(this).val());
});
});
if (values.length == 6) {
alert("Hello! I am an alert box!!");
}
HTML
<div>
<button name="btn1" type="submit" id="btn1" value="1" class="ui-btn"></button>
<button name="btn2" type="submit" id="btn2" value="2" class="ui-btn"></button>
</div> // I have up to 9 buttons, thats only a piece
Upvotes: 0
Views: 3326
Reputation: 17351
All you have to do is put the if
statement inside the click()
jQuery event handler, like so:
$('.ui-btn').click(function() {
values.push($(this).val());
if (values.length == 6) {
alert("Hello! I am an alert box!!");
}
});
See working example at JSFiddle.net.
The problem with your code is that the if
statement is only executed once, at the beginning, to check if the length of the array is 6. It needs to be run every time a button is clicked (therefore inside the click
handler).
Upvotes: 3