
Untitled
By: a guest on
Aug 11th, 2012 | syntax:
None | size: 1.53 KB | hits: 8 | expires: Never
Value returning undefined if used in function
<div class="one" data-selection="1"></div>
<p class="selections"></p>
<p class="cost">£ <span class="price">0</span></p>
<script>
// Empty array to capture selections
var a = [];
var originalValue = "You have Selected section(s): ";
$(".one").click(function () {
if ($(this).attr('class') == "selected one") {
//alert
alert("Are you sure you wish to de-select?");
//remove from the total
$(".price").html(parseInt($(".price").html(),10) - 30)
$(this).removeClass("selected ");
// need to get remove from array
var removeItem = $(this).attr("data-selection");
a = jQuery.grep(a, function(value) {
return value != removeItem;
});
$('.selections').text(originalValue + a);
}
else {
var selection = $(this).attr("data-selection");
alert(selection);
var originalSrc = $(this).attr('class');
$(this).attr('class', 'selected ' + originalSrc);
a.push(1);
a.sort();
$('.selections').text(originalValue + a);
$('.price').text(function(i, txt) {
return +txt + 30;
});
}
});
</script>
undoSelection()
$(".one").click(function () {
if ($(this).attr('class') == "selected one") {
undoSelection();
}
else {
$(this).attr("data-selection");