jQuery - accessing variable outside onchange event? [duplicate] - javascript

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I'm trying to make my code dependent on certain variables by changing them with a select. Is it possible to access the result outside of the function in jQuery?
js
function myCalendar(){
//global variable
var thismonth = '';
//onchange get selected month
$(document).on('change','#select_month',function(){
thismonth = $(this).val();
});
//check if empty
if(thismonth !==''){//always return empty?
var mm = thismonth;
}else{
var mm = new Date().getMonth();
}
}

Related

undefine variable Displayed when implement below code [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 months ago.
undefine variable will loadData function from displayStructure all variables names, ages..etc displayed
async function loadData(route) {
const response = await fetch('http://127.0.0.1:8000/'+route);
const names =await response.json();
console.log(names);
return names;
}
function displayStructure(){
var names=loadNames('getNames');
var department=loadNames('getDep');
var ages=loadNames('getAge');
var bod=loadNames('getbod');
}

How to access event.clientX outside of window function? [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 months ago.
I tried to use the event.clientX outside of window function. But it says xAxis is not defined. Can have an way to get it? My code below,
window.onclick = function(event) {
let xAxis = event.clientX;
}
alert(xAxis);

JavaScript and geolocation [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 1 year ago.
)
So I'm trying to learn about JavaScript and geolocation, but I've run into this, probably simple, problem:
let myLat; // Global
navigator.geolocation.getCurrentPosition((position) => {
myLat = position.coords.latitude;
console.log(myLat); // works
});
console.log(myLat); // undefined
I don't get why I cannot just use a global variable to hold the latitude-info?

javascript boolean variable's value is not changing [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I have a variable called itemExist and set that equal to false. My goal is to set this variable's value to true if a condition happens. My code is this:
var itemExist = false;
user.findById({_id: decoded._id}).populate('shoppingCart').exec((err, items)=>{
items.shoppingCart.forEach(async item=>{
if(item.productId == productId){
itemExist = true;
When I console log the itemExist variable outside of all theese functions
console.log(itemExist);
I get that result on the console:
false
But it should be true. How can I solve this problem, what is causing that?
Perform your work inside exec function
user.findById({_id: decoded._id}).populate('shoppingCart').exec((err, items)=>
{
const itemInArr = items.shoppingCart.find(item => item.productId === productId);
//!! - convertation to boolead value
itemExist = !!itemInArr;
console.log(itemExist);
}
);

Javascript json response undefined [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have function like this:
function GetLala() {
var lala = $.getJSON(uriLala).done(function (data) {
return data;
});
return lala;
}
Then code like this:
var data = GetLala();
console.log(data.responseJSON);
This returned me undefined. If I type this code in google chrome console - then work.

Categories

Resources