I have the following function:
simpleModal: function (data, id) {
var responseHtml = data;
// Append the modal HTML to the DOM
var modalInstance = $('body').append(modalHtml);
$(modalInstance).attr('id', id);
$(id).find('.uiModalContent').width(480);
$(id).find('.uiModalContent').height(320);
// Hide the modal straight away, center it, and then fade it in
$(id).find('.uiModal').hide().center().fadeIn();
// Dynamically load in the passed data from the call
$(id).find('.uiModalContent').html($(responseHtml));
$(id).find('.uiModalContent').removeClass('loading');
$(id).find('.ModalClose').live('click', function (e) {
e.preventDefault();
$(this).parents('.uiModal').fadeOut(function () { $(this).parents('.uiModalWrapper').remove() });
});
},
when called like:
uiModal.simpleModal('<p>An error has occured</p>','TestError');
it should append the modal to the body with the passed content and give the modalHtml an id that is also passed. However it gets confused to appends the id to the body instead of the html. How do I get around this? Thanks
This is because the
appendmethod returns the element you are appending to, rather than the element you’re appending.Instead, you can use the
appendTomethod, and use it as follows;You also need to be using
$('#' + id)as the ID selector as opposed to$(id); otherwise you’ll end up looking for all elements with the tag name ofTestError.Additionally, you should seriously consider caching the result of
$('#' + id); you’re performing the same DOM lookup operation 6 times; which is totally unnecessary, as you have exactly the same jQuery object cached invar modalInstance; replace all instances of$('#' + id)tomodalInstanceThis point applies to
$(id).find('.uiModalContent')as well; cache it!Although you can also chain your methods for the same result;