JavaScript Image visibility using javascript

I want to toggle the image using if-else but it didn't work, can someone please explain what is wrong with the code.

<img src="img/somepic.jpg" id="pic"><br/>

<a target='_blank' href="javascript:toggleImg()">Toggle Image</a>

<script>

function toggleImg(){
    var img = document.getElementById('pic');

    var checkForVisible = img.style.visibility == "visible";

    if(checkForVisible == "visible"){
        img.style.visibility = "hidden";
    }
    else{
        img.style.visibility = "visible";
    }

}

</script>

I tried using "ternary operator"

img.style.visibility = checkForVisible ? "hidden" : "visible";

and it worked. I just want to know about the "if-else" code which doesn't work. Thanks

Answer:1

You do this:

var checkForVisible = img.style.visibility == "visible";

The == "visible" makes it the result of the equals expression so it's going to be true or false.

Your if/else is checkForVisible == "visible" which won't work because is will only be true or false. You only need to compare once.

if(img.style.visibility == "visible"){
        img.style.visibility = "hidden";
    }
    else{
        img.style.visibility = "visible";
    }
Answer:2

Use toggle instead of if else.

function toggleImg() {
  var img = document.getElementById('pic');
  img.classList.toggle('hidden');

}
.visible {
  display: block;
}
.hidden {
  display: none;
}
<img src="img/somepic.jpg" id="pic">
<br/>

<a target='_blank' href="javascript:toggleImg()">Toggle Image</a>
Answer:3

I'm a beginner in angular test with jasmine and i need to test data type(e.g. String, int, etc.) This is my controller, my data is instantiated to null and later it will be a string : _this....

I'm a beginner in angular test with jasmine and i need to test data type(e.g. String, int, etc.) This is my controller, my data is instantiated to null and later it will be a string : _this....

The Full code of my Java Encryption/Decryption algorithm: public class AESEncryptUtil { private static AESEncryptUtil instance = new AESEncryptUtil(); private String password = "123456"; ...

The Full code of my Java Encryption/Decryption algorithm: public class AESEncryptUtil { private static AESEncryptUtil instance = new AESEncryptUtil(); private String password = "123456"; ...

  1. react native encryption library
  2. react native encryption example
  3. react native encryption
  4. react native encryption rsa
  5. react native encrypted storage
  6. react native encrypt password
  7. react native encrypt file
  8. react native encrypt data
  9. react native encrypt string
  10. react native encrypt decrypt
  11. react native encrypted local storage
  12. react native encrypted database
  13. react native encrypted async storage
  14. react native encrypt source code
  15. react native aes encryption
  16. react native asyncstorage encryption
  17. react native sqlite encryption
  18. react-native-simple-encryption
  19. react native realm encryption
  20. react native video encryption

I want to join two objects with functions into a merged object using ES6, not sure how to do this. For example, I'd like to merge these two objects: const first = { first: function() { return {...

I want to join two objects with functions into a merged object using ES6, not sure how to do this. For example, I'd like to merge these two objects: const first = { first: function() { return {...

  1. merge objects with same key javascript
  2. merge objects with spread operator
  3. merge objects with arrays javascript
  4. merge objects with same key
  5. merge objects with same id
  6. merge objects with automapper
  7. merge objects with javascript
  8. merge objects with same property
  9. merge objects with array
  10. merge objects with
  11. merge objects with blender
  12. merge objects in illustrator
  13. merge objects by key javascript
  14. merge objects in powerpoint
  15. merge objects in 3ds max
  16. merge objects in word
  17. merge objects in inkscape
  18. merge objects in cinema 4d
  19. merge objects in array lodash
  20. merge objects in js

Why does this code produce an array of 20 elements which are all undefined: > Array(20).map(function() { return 3; }) [ , , , , , , , , , , , , , , , , , , , ] Whereas this code produces an ...

Why does this code produce an array of 20 elements which are all undefined: > Array(20).map(function() { return 3; }) [ , , , , , , , , , , , , , , , , , , , ] Whereas this code produces an ...