These are the two functions in my program.
Function 1:
var parentLI; //Global variable so can be accessed in both functions
$(document).on("click", ".addmorebrands", function() {
parentLI = $(this).closest('li'); //Will the value of parentLI local to function 1 only ?
$('#exampleModalCenter').modal('show');
$('#exampleModalCenter img').click(function() {
parentdiv = $(this).closest('div.outerdiv'); //Varbiale local to function 1
parentdiv.addClass('preselect');
parentdiv.siblings().removeClass('preselect');
selectedImageSRC = $(this).attr('src'); //Varbiale local to function 1
})
});
Function 2:
$('#add-image').click(function(){
parentLI.append('<div class="imagewrap"><img class="images" src="'+selectedImageSRC+'" title="Manual Addition"> <input type="button" class="removediv" value="X" /></div>');
var imageNameValue = parentLI.children('.imagenames').val();
var imageTitleValue = parentLI.children('.hoverinformation').val();
$('#exampleModalCenter').modal('hide');
parentdiv.removeClass('preselect'); //How am I able to access parentdiv which is local to function 1
});
Doubts I have regarding scope of variable
- The
parentdivis defined inFunction1and will be local toFunction1. How am I able to access it inFunction2 - The
selectedImageSRCis defined inFunction1and will be local toFunction1. How am I able to accesss it inFunction2In the lineparentLI.append('<div class="imagewrap"><img class="images" src="'+selectedImageSRC+'" title="Manual Addition"> <input type="button" class="removediv" value="X" /></div>'); - The variable
parentLIis declared outside both the functions so it will be global.But its value($(this).closest('li');) is assigned to it inFunction1.How is that value($(this).closest('li');) accesible inFunction2