I have a click hander that handles these loop functions
$('.container').on('click', '.folder', function(e){
var ClickedJobId = $(this).closest("div").prop('id');
JobId.value = ClickedJobId;
alert(ClickedJobId);
console.log(ClickedJobId);
$('.gallery').fadeOut('fast',function() {
$('.applications').fadeIn('fast');
})
})
this firebase auth state change determines the child path of the two functions below.
firebase.auth().onAuthStateChanged((user) => {
if (user) {
database = firebase.database();
var BusinessesId = firebase.auth().currentUser.uid;
var selectedJob = JobId.value ;
var jobref = database.ref('/Jobs/');
jobref.on('value', JobData, errData);
var appref = database.ref('/Jobs/' + ClickedJobId + '/Applications/')
appref.on('value', ApplicationData, errData);
}
})
function JobData pulls all the entries from firebase DB /Jobs/
function JobData(data) {
var container = document.getElementById('Jobs');
data.forEach(function(JobSnap) { // loop over all jobs
var key = JobSnap.key;
var Jobs = JobSnap.val();
var jobCard = `
<div class="thumbnail" id="${key}">
<span class="folder"><span class="file"></span></span>
<div class="title" id="Jobs">${Jobs.JobTitle}</div>
</div>
`;
container.innerHTML += jobCard;
})
}
and function ApplicationData should be pulling from '/Jobs/' + ClickedJobId + '/Applications/'
function ApplicationData(data) {
........
....
}
The problem I am having is that I can't figure out a way to use ClickedJobId in another function especially ApplicationData. ClickedJobId was defined in the click handler and whenever I reference it in applicationData is comes out as undefined, how can I use ClickedJobId in ApplicationData?
var ClickedJobId="";outside the jQeery ready, then change the inside assignment toClickedJobId = $(this).closest("div").prop('id');windowis the global object you can assign properties to.