JavaScript getElementById().value Returning Undefined getelementbyid not returning value

I'm writing some simple code to change the visibility of an image when a button is clicked, but my document.getElementById().value is coming up as undefined. (I've tried replacing .value with .display - same result).

What could be the problem?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <script>
        function hideImage(imageId){
            document.getElementById(imageId).visibility="hidden";
        }
        function showImage(imageId){
            document.getElementById(imageId).visibility="visible";
        }
        function switchVis(imageId){
            var curVis = document.getElementById(imageId).value;
            if(document.getElementById(imageId).value=="hidden"){
                document.getElementById("output").innerHTML="hidden";
                showImage(imageId);
            }
            else if(document.getElementById(imageId).value=="visible"){
                hideImage(imageId);
                document.getElementById("output").innerHTML="visible";
            }
            else{
                alert("Visibility Issue!\nVisibility value is " + curVis);
            }
        }
    </script>
</head>
<body>
    <img src="images/k3.gif" id="k3"><p>
    <button onclick=switchVis('k3')>Visibility</button>
    <div id="output"></div>
</body>

Answer:1

You cannot simply get style value by using document.getElementById().value . getComputedStyle() gives the final used values of all the CSS properties of an element. The returned style is a CSSStyleDeclaration object which can be used to get the value of your style. Try follwing code. It should work.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 <script>
    function hideImage(imageId){
        document.getElementById(imageId).style.visibility="hidden";
    }
    function showImage(imageId){
        document.getElementById(imageId).style.visibility="visible";
    }
    function switchVis(imageId){
        var curVis = document.getElementById(imageId); //your element
        style = window.getComputedStyle(curVis); //returns CSSStyleDeclaration object
        curVis = style.getPropertyValue('visibility'); //now get your css value

        if(curVis=="hidden"){
            document.getElementById("output").innerHTML="visible";
            showImage(imageId);
        }
        else if(curVis=="visible"){
            hideImage(imageId);
            document.getElementById("output").innerHTML="hidden";
        }
        else{
            alert("Visibility Issue!\nVisibility value is " + curVis);
        }
    }
 </script>
</head>
<body>
    <img src="images/k3.gif" id="k3" value="check"><p>
    <button onclick="switchVis('k3')">Visibility</button>
    <div id="output"></div>
</body>
Answer:2

You can get visibility like that: jsfiddle So it's in document.getElementById('myId').style.visibility

//to make visible
document.getElementById('myId').style.visibility='visible'

//to make hidden
document.getElementById('myId').style.visibility='hidden'

Instead of value you used there's innerHTML. Value is for input elements.

Answer:3

try this

to show

  document.getElementById('k3').style.visibility='visible';

to hide

 document.getElementById('k3').style.display='none';

refer this for better idea

Answer:4

I want to move items between two Listboxes in ASP.Net using JQuery/Javascript and below is my code which is working perfectly. function AddItems() { var totalItemsSelected = 0; var ...

I want to move items between two Listboxes in ASP.Net using JQuery/Javascript and below is my code which is working perfectly. function AddItems() { var totalItemsSelected = 0; var ...

  1. move items between sharepoint lists
  2. move items between vaults 1password
  3. move items between characters destiny 2
  4. move items between two select lists
  5. move items between calendars in outlook
  6. move items between layers indesign
  7. move items between two listbox c#
  8. move items between settlements fallout 4
  9. move items between listboxes c#
  10. move items between listboxes vba
  11. move items between listboxes
  12. move items between two listboxes javascript
  13. move items between folders in sharepoint
  14. move items between keychains
  15. move items between two select lists bootstrap
  16. move items between lists angular
  17. move items between list view javafx
  18. zotero move items between collections
  19. rimworld move items between stockpiles
  20. bdo move items between characters

I'm trying to do a js app that would basically move some balls on a canvas element. I set the context.fillStyle = "rgba(12, 34, 56, 0.2)"; the problem is that the balls become opaque from transparent ...

I'm trying to do a js app that would basically move some balls on a canvas element. I set the context.fillStyle = "rgba(12, 34, 56, 0.2)"; the problem is that the balls become opaque from transparent ...

I am attempting to write a pair of shaders for WebGL which will allow me to render a color cube. However, when I attempt to open the file, I receive the error "unable to initialize the shader program"...

I am attempting to write a pair of shaders for WebGL which will allow me to render a color cube. However, when I attempt to open the file, I receive the error "unable to initialize the shader program"...

I have a question concerning plugins. I am using the ionic framework (http://ionicframework.com/) and Cordova for my hybrid app. I am using a toast (text popup) plugin to provide some feedback. ...

I have a question concerning plugins. I am using the ionic framework (http://ionicframework.com/) and Cordova for my hybrid app. I am using a toast (text popup) plugin to provide some feedback. ...

  1. cordova ionic plugin keyboard
  2. cordova ionic plugin
  3. cordova ionic plugin webview
  4. cordova ionic plugin_not_installed
  5. cordova ionic plugin camera
  6. ionic cordova plugin remove
  7. ionic cordova plugin add
  8. cordova-plugin-ionic-webview npm
  9. cordova-plugin-ionic-keyboard example
  10. cordova-plugin-ionic@^5.0.0
  11. ionic cordova plugin update
  12. ionic cordova-plugin-whitelist
  13. ionic cordova-plugin-file
  14. ionic cordova plugin list
  15. ionic cordova-plugin-firebase
  16. cordova-plugin-ionic-keyboard npm
  17. ionic cordova-plugin-device
  18. cordova-plugin-ionic-webview example
  19. ionic cordova-plugin-advanced-http
  20. ionic cordova-plugin-googlemaps