JSFIDDLE: http://jsfiddle.net/nqsfoqzz/10/
I'm having trouble figuring out what the this
selector is in my code below. If I call the function removeAndClose(elem); as shown near the end of the click event (currently commented out), the value of $(this) is correct, which is .removeleg
. But I need to control the removeAndClose() function based on a click event in a popup, which is the reason for $noticeMessage within the click event, as well as the reason for the function noticePopup(). I have removeAndClose(elem) added to the Confirm
button, which is where the this
problem comes up.
Since I'm using the removeAndClose() function within $noticeMessage, elem is undefined
.
I've put together a jsfiddle that shows the problem: http://jsfiddle.net/nqsfoqzz/10/ To see the CONFIRM button, first click the 'ADD LEG' button. Then click 'Remove Leg' from the newly added element. You should then see the popup to confirm or cancel the removal.
Main Click Event:
$('.missionlegswrapper').on('click', '.removeleg', function(e){
e.preventDefault();
var elem = $(this);
// Notice Message
$noticeMessage =
'<div>' +
'<div>Are you sure you want to remove this leg? All the information you\'ve entered will be lost.</div>' +
'<div>' +
'<a href="javascript:void(0);" class="button confirm green" onClick="removeAndClose(elem);">Confirm</a>' +
'<a href="javascript:void(0);" class="button cancel red" onClick="closePopup();">Cancel</a>' +
'</div>' +
'<div class="clear"></div>' +
'</div>';
// Notice Popup
noticePopup('error',$noticeMessage);
//removeAndClose(elem);
});
Additional Functions:
/**
* Notice Popup Function
* messageType: The message to be entered into the popup
* message: error, notice, warning (services as class name identifier)
*/
function noticePopup(messagetype,message){
// Append Backdrop to Body
$('body').append('<div class="noticepopupbackdrop"></div>');
// Append Popup to Body
$('body').append(
'<div class="noticepopup ' + messagetype + '">' +
'<div class="noticepopup_outer">' +
'<div class="noticepopup_inner">' +
'<div class="noticepopup_header">' +
'<span>Attention</span>' +
'<div class="noticeclosebttn"></div>' +
'</div>' +
'<div class="noticepopup_content">' +
'<div class="noticepopupcontent_inner">' +
message +
'<div class="clear"></div>' +
'</div>' +
'<div class="clear"></div>' +
'</div>' +
'<div class="clear"></div>' +
'</div>' +
'<div class="clear"></div>' +
'</div>' +
'<div class="clear"></div>' +
'</div>'
);
// Initial Open
$('div.noticepopupbackdrop').fadeIn(150);
$('div.noticepopup').css({display: 'block'}).animate({top: '50%', opacity: '1.00'}, 300);
// Grab Width and Height and Center Popup on Page Load
popupWidthHeight();
// On Resize
$(window).on('resize', function(){
// Grab Width and Height and Center Popup on Resize
popupWidthHeight();
});
// On Click Close
$('div.noticepopupbackdrop,div.noticeclosebttn').on('click', function(){
closePopup();
});
}
/**
* Close Popup Function
*/
function closePopup(){
$('div.noticepopup').fadeOut(150);
$('div.noticepopupbackdrop').fadeOut(175);
setTimeout(function(){
$('div.noticepopupbackdrop,div.noticepopup').remove();
}, 350);
}
/**
* Get Notice Popup Width and Height Function
*/
function popupWidthHeight(){
var noticeWidth = $('div.noticepopup').width();
var noticeHeight = $('div.noticepopup').height();
$('div.noticepopup').css({marginTop: -(noticeHeight / 2), marginLeft: -(noticeWidth / 2) });
}
/**
* Close Popup Notice and Remove Mission Leg
*/
function removeAndClose(thisObj){
thisObj.closest('.missionlegitem').remove();
console.log(thisObj);
// Close Popup
var popupCloseTime = setTimeout(function(){
closePopup();
},25);
}