0

I have a

var imgSrc = false;
dom.getElByTag('img')[0].onclick = function(){
  //set src of image
  changeBool(dom.getElByTag('img')[0]);
}

function changeBool(n){
    if(n.src ===="file/path"){
    imgSrc = true;
};

console.log(imgSrc);

So the problem is that imgSrc is only changed locally in changeBool so if I console.log imgSrc it will return false, regardless of where I put it (this is likely because the page has already loaded and decided that imgSrc is false before I set it to true with the onclick, and since javascript runs one line at a time it has run console.log(imgSrc) as false before I could even click the image.

So, I need to:

declare a boolean as false -> click an image, which sets boolean as true -> console.logging the boolean (and seeing true)

But what I am doing right now is: declare a boolean as false -> console.logging the boolean (and seeing false {because it hasn't been set to true yet} - > clicking an image and setting the boolean to true.

How can I fix this?

1

2 Answers 2

1

Just move your logging statement inside the changeBool function like so:

var imgSet = false;
dom.getElByTag('img')[0].onclick = function(){
  //set src of image
  changeBool(dom.getElByTag('img')[0]);
}

function changeBool(n){
    if(n.src ===="file/path") imgSrc = true;
    console.log(imgSrc);
};
Sign up to request clarification or add additional context in comments.

Comments

1

In your first line of code,

var imgSet = false;

Needs to be changed to

var imgSrc = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.