Reputation: 931
I have a link something like this
<a onclick="returnCategory($(this).attr('data-category-id'))" data-category-id="15">Tablets</a>
and the function called using onclick is
function returnCategory(categoryId) {
return categoryId;
}
my fancybox code is
function showPopup() {
$.fancybox({
href: 'popup.php',
type: 'ajax',
width: '90%',
height: '100%',
beforeClose : function () {
categoryId = returnCategory();
alert("Required category Id is :" + categoryId);
},
});
}
Clicking on the anchor the above code generates an alert "Required category Id is : undefined"
My question is how to alert the value returned by returnCategory() function in fancybox beforeClose? I don't want to use global variable here.
Upvotes: 0
Views: 62
Reputation: 94
Your problem is that the function returnCategory
returns the value that was passed to it. So when you call it in fancybox beforeClose event without passing a parameter it will return undefined
. If you really can't user global variables one way of doing it is with hidden inputs like this
<input type="hidden" id="selectedCategory"/>
and the javascript
function returnCategory(categoryId) {
var inputCategory = document.getElementById("selectedCategory");
inputCategory.value = categoryId;
}
and
beforeClose:function(){
selectedCategoryId = document.getElementById("selectedCategory").value;
alert("Required category Id is :" + selectedCategoryId );
}
Upvotes: 1