How to change visibility css property using DOM when mouseover









up vote
0
down vote

favorite












I created an <img/> element from js and i want it to appear only when mouseover

The callback function makesVisible() is actually called but nothing is change.



I would like to change visibility from hidden to visible



var imgHover = document.createElement('img');
imgHover.setAttribute("src", "img/icona_play.jpg");
imgHover.style.width = "30px";
imgHover.style.height = "23px";
imgHover.style.position = "absolute";
imgHover.style.margin = "0 auto";
imgHover.style.left = "45px";
imgHover.style.bottom = "35px";
//I want to change this following property
imgHover.style.visibility = "hidden";
imgContainer.appendChild(imgHover);

//Calling the function when mouseover
imgContainer.addEventListener("mouseover", makeVisible, false);


function makeVisible()

imgHover.style.visibility = "visible";










share|improve this question























  • imgHover.style.display = 'unset' to show, and imgHover.style.display = 'none' to hide
    – Olian04
    Nov 11 at 20:08











  • nothing changes using display prop
    – Matteo Bruni
    Nov 11 at 20:27















up vote
0
down vote

favorite












I created an <img/> element from js and i want it to appear only when mouseover

The callback function makesVisible() is actually called but nothing is change.



I would like to change visibility from hidden to visible



var imgHover = document.createElement('img');
imgHover.setAttribute("src", "img/icona_play.jpg");
imgHover.style.width = "30px";
imgHover.style.height = "23px";
imgHover.style.position = "absolute";
imgHover.style.margin = "0 auto";
imgHover.style.left = "45px";
imgHover.style.bottom = "35px";
//I want to change this following property
imgHover.style.visibility = "hidden";
imgContainer.appendChild(imgHover);

//Calling the function when mouseover
imgContainer.addEventListener("mouseover", makeVisible, false);


function makeVisible()

imgHover.style.visibility = "visible";










share|improve this question























  • imgHover.style.display = 'unset' to show, and imgHover.style.display = 'none' to hide
    – Olian04
    Nov 11 at 20:08











  • nothing changes using display prop
    – Matteo Bruni
    Nov 11 at 20:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I created an <img/> element from js and i want it to appear only when mouseover

The callback function makesVisible() is actually called but nothing is change.



I would like to change visibility from hidden to visible



var imgHover = document.createElement('img');
imgHover.setAttribute("src", "img/icona_play.jpg");
imgHover.style.width = "30px";
imgHover.style.height = "23px";
imgHover.style.position = "absolute";
imgHover.style.margin = "0 auto";
imgHover.style.left = "45px";
imgHover.style.bottom = "35px";
//I want to change this following property
imgHover.style.visibility = "hidden";
imgContainer.appendChild(imgHover);

//Calling the function when mouseover
imgContainer.addEventListener("mouseover", makeVisible, false);


function makeVisible()

imgHover.style.visibility = "visible";










share|improve this question















I created an <img/> element from js and i want it to appear only when mouseover

The callback function makesVisible() is actually called but nothing is change.



I would like to change visibility from hidden to visible



var imgHover = document.createElement('img');
imgHover.setAttribute("src", "img/icona_play.jpg");
imgHover.style.width = "30px";
imgHover.style.height = "23px";
imgHover.style.position = "absolute";
imgHover.style.margin = "0 auto";
imgHover.style.left = "45px";
imgHover.style.bottom = "35px";
//I want to change this following property
imgHover.style.visibility = "hidden";
imgContainer.appendChild(imgHover);

//Calling the function when mouseover
imgContainer.addEventListener("mouseover", makeVisible, false);


function makeVisible()

imgHover.style.visibility = "visible";







javascript css dom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 8:06









vsync

45.1k35157217




45.1k35157217










asked Nov 11 at 19:57









Matteo Bruni

366




366











  • imgHover.style.display = 'unset' to show, and imgHover.style.display = 'none' to hide
    – Olian04
    Nov 11 at 20:08











  • nothing changes using display prop
    – Matteo Bruni
    Nov 11 at 20:27

















  • imgHover.style.display = 'unset' to show, and imgHover.style.display = 'none' to hide
    – Olian04
    Nov 11 at 20:08











  • nothing changes using display prop
    – Matteo Bruni
    Nov 11 at 20:27
















imgHover.style.display = 'unset' to show, and imgHover.style.display = 'none' to hide
– Olian04
Nov 11 at 20:08





imgHover.style.display = 'unset' to show, and imgHover.style.display = 'none' to hide
– Olian04
Nov 11 at 20:08













nothing changes using display prop
– Matteo Bruni
Nov 11 at 20:27





nothing changes using display prop
– Matteo Bruni
Nov 11 at 20:27













3 Answers
3






active

oldest

votes

















up vote
0
down vote













You have an option of using an opacity property instead.
Initially set it like so: imgHover.style.opacity = 0;
Than in the makeVisible method change it to imgHover.style.opacity = 1;



Another solution to this problem would be setting addEventListener method on the container div. Assuming that you can have a container around the image with exactly the same dimensions as the Image.



For example:



imgContainer.addEventListener("mouseover", makeVisible, false);


The thing is that opacity and visibility will act the same in a sense of not collapsing the space that the element should occupy. What is different though that hidden element will ignore mouse/pointer events.






share|improve this answer





























    up vote
    0
    down vote













    Your code works as it should provided that you set up a valid reference to imgContainer and that you set a valid path to an image for the dynamically created element:






    var imgContainer = document.getElementById("container");
    var imgHover = document.createElement('img');
    imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
    imgHover.style.width = "30px";
    imgHover.style.height = "23px";
    imgHover.style.position = "absolute";
    imgHover.style.margin = "0 auto";
    imgHover.style.left = "45px";
    imgHover.style.bottom = "35px";
    imgHover.style.visibility = "hidden";
    imgContainer.appendChild(imgHover);

    imgContainer.addEventListener("mouseover", makeVisible, false);

    function makeVisible()
    imgHover.style.visibility = "visible";

    <div id="container">Hover Over Me</div>





    Having said that, you should avoid setting inline styles on elements as they are hard to override when needed and they often cause duplication of code. It's much simpler to set up CSS classes ahead of time and just apply/remove those classes as needed with the element.classList API.



    Also, visibility does affect whether you see an element or not, but even when you don't see it, space is allocated in the UI for it, which isn't always desirable. In most cases, using a display:none to hide an element and then simply removing that instruction to show the element is the better approach.



    Lastly, while using setAttribute() is certainly valid, you can also configure your elements via their direct properties. Almost all HTML attributes map to a corresponding JavaScript object property. Using these can make the code much simpler.



    Take a look at an example that puts all this together:






    var imgContainer = document.getElementById("container");
    var imgHover = document.createElement('img');

    // Just set properties of the element directly:
    imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

    // Just add pre-made classes to style the element
    imgHover.classList.add("hoverImg");
    imgHover.classList.add("hidden");

    imgContainer.appendChild(imgHover);
    imgContainer.addEventListener("mouseover", makeVisible);
    function makeVisible()
    imgHover.classList.remove("hidden");;

    .hidden display:none; /* Used when an element needs to be hidden */

    /* This will be applied via JS */
    .hoverImg
    width:30px;
    height:23px;
    position: absolute;
    margin:0 auto;
    left:45px;
    bottom:35px;

    <div id="container">Hover Over Me</div>








    share|improve this answer



























      up vote
      -1
      down vote













      Here you were appending element like this 
      imgContainer.appendChild(imgHover);
      So reference for imgHover element in dom will get
      change. Fetch that element once again inside
      makeVisible() function.
      document.querySelector("img") // use your appropriate.





      share|improve this answer




















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252652%2fhow-to-change-visibility-css-property-using-dom-when-mouseover%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        You have an option of using an opacity property instead.
        Initially set it like so: imgHover.style.opacity = 0;
        Than in the makeVisible method change it to imgHover.style.opacity = 1;



        Another solution to this problem would be setting addEventListener method on the container div. Assuming that you can have a container around the image with exactly the same dimensions as the Image.



        For example:



        imgContainer.addEventListener("mouseover", makeVisible, false);


        The thing is that opacity and visibility will act the same in a sense of not collapsing the space that the element should occupy. What is different though that hidden element will ignore mouse/pointer events.






        share|improve this answer


























          up vote
          0
          down vote













          You have an option of using an opacity property instead.
          Initially set it like so: imgHover.style.opacity = 0;
          Than in the makeVisible method change it to imgHover.style.opacity = 1;



          Another solution to this problem would be setting addEventListener method on the container div. Assuming that you can have a container around the image with exactly the same dimensions as the Image.



          For example:



          imgContainer.addEventListener("mouseover", makeVisible, false);


          The thing is that opacity and visibility will act the same in a sense of not collapsing the space that the element should occupy. What is different though that hidden element will ignore mouse/pointer events.






          share|improve this answer
























            up vote
            0
            down vote










            up vote
            0
            down vote









            You have an option of using an opacity property instead.
            Initially set it like so: imgHover.style.opacity = 0;
            Than in the makeVisible method change it to imgHover.style.opacity = 1;



            Another solution to this problem would be setting addEventListener method on the container div. Assuming that you can have a container around the image with exactly the same dimensions as the Image.



            For example:



            imgContainer.addEventListener("mouseover", makeVisible, false);


            The thing is that opacity and visibility will act the same in a sense of not collapsing the space that the element should occupy. What is different though that hidden element will ignore mouse/pointer events.






            share|improve this answer














            You have an option of using an opacity property instead.
            Initially set it like so: imgHover.style.opacity = 0;
            Than in the makeVisible method change it to imgHover.style.opacity = 1;



            Another solution to this problem would be setting addEventListener method on the container div. Assuming that you can have a container around the image with exactly the same dimensions as the Image.



            For example:



            imgContainer.addEventListener("mouseover", makeVisible, false);


            The thing is that opacity and visibility will act the same in a sense of not collapsing the space that the element should occupy. What is different though that hidden element will ignore mouse/pointer events.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 21:03

























            answered Nov 11 at 20:43









            Alex Bartfeld

            13




            13






















                up vote
                0
                down vote













                Your code works as it should provided that you set up a valid reference to imgContainer and that you set a valid path to an image for the dynamically created element:






                var imgContainer = document.getElementById("container");
                var imgHover = document.createElement('img');
                imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
                imgHover.style.width = "30px";
                imgHover.style.height = "23px";
                imgHover.style.position = "absolute";
                imgHover.style.margin = "0 auto";
                imgHover.style.left = "45px";
                imgHover.style.bottom = "35px";
                imgHover.style.visibility = "hidden";
                imgContainer.appendChild(imgHover);

                imgContainer.addEventListener("mouseover", makeVisible, false);

                function makeVisible()
                imgHover.style.visibility = "visible";

                <div id="container">Hover Over Me</div>





                Having said that, you should avoid setting inline styles on elements as they are hard to override when needed and they often cause duplication of code. It's much simpler to set up CSS classes ahead of time and just apply/remove those classes as needed with the element.classList API.



                Also, visibility does affect whether you see an element or not, but even when you don't see it, space is allocated in the UI for it, which isn't always desirable. In most cases, using a display:none to hide an element and then simply removing that instruction to show the element is the better approach.



                Lastly, while using setAttribute() is certainly valid, you can also configure your elements via their direct properties. Almost all HTML attributes map to a corresponding JavaScript object property. Using these can make the code much simpler.



                Take a look at an example that puts all this together:






                var imgContainer = document.getElementById("container");
                var imgHover = document.createElement('img');

                // Just set properties of the element directly:
                imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

                // Just add pre-made classes to style the element
                imgHover.classList.add("hoverImg");
                imgHover.classList.add("hidden");

                imgContainer.appendChild(imgHover);
                imgContainer.addEventListener("mouseover", makeVisible);
                function makeVisible()
                imgHover.classList.remove("hidden");;

                .hidden display:none; /* Used when an element needs to be hidden */

                /* This will be applied via JS */
                .hoverImg
                width:30px;
                height:23px;
                position: absolute;
                margin:0 auto;
                left:45px;
                bottom:35px;

                <div id="container">Hover Over Me</div>








                share|improve this answer
























                  up vote
                  0
                  down vote













                  Your code works as it should provided that you set up a valid reference to imgContainer and that you set a valid path to an image for the dynamically created element:






                  var imgContainer = document.getElementById("container");
                  var imgHover = document.createElement('img');
                  imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
                  imgHover.style.width = "30px";
                  imgHover.style.height = "23px";
                  imgHover.style.position = "absolute";
                  imgHover.style.margin = "0 auto";
                  imgHover.style.left = "45px";
                  imgHover.style.bottom = "35px";
                  imgHover.style.visibility = "hidden";
                  imgContainer.appendChild(imgHover);

                  imgContainer.addEventListener("mouseover", makeVisible, false);

                  function makeVisible()
                  imgHover.style.visibility = "visible";

                  <div id="container">Hover Over Me</div>





                  Having said that, you should avoid setting inline styles on elements as they are hard to override when needed and they often cause duplication of code. It's much simpler to set up CSS classes ahead of time and just apply/remove those classes as needed with the element.classList API.



                  Also, visibility does affect whether you see an element or not, but even when you don't see it, space is allocated in the UI for it, which isn't always desirable. In most cases, using a display:none to hide an element and then simply removing that instruction to show the element is the better approach.



                  Lastly, while using setAttribute() is certainly valid, you can also configure your elements via their direct properties. Almost all HTML attributes map to a corresponding JavaScript object property. Using these can make the code much simpler.



                  Take a look at an example that puts all this together:






                  var imgContainer = document.getElementById("container");
                  var imgHover = document.createElement('img');

                  // Just set properties of the element directly:
                  imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

                  // Just add pre-made classes to style the element
                  imgHover.classList.add("hoverImg");
                  imgHover.classList.add("hidden");

                  imgContainer.appendChild(imgHover);
                  imgContainer.addEventListener("mouseover", makeVisible);
                  function makeVisible()
                  imgHover.classList.remove("hidden");;

                  .hidden display:none; /* Used when an element needs to be hidden */

                  /* This will be applied via JS */
                  .hoverImg
                  width:30px;
                  height:23px;
                  position: absolute;
                  margin:0 auto;
                  left:45px;
                  bottom:35px;

                  <div id="container">Hover Over Me</div>








                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Your code works as it should provided that you set up a valid reference to imgContainer and that you set a valid path to an image for the dynamically created element:






                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');
                    imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
                    imgHover.style.width = "30px";
                    imgHover.style.height = "23px";
                    imgHover.style.position = "absolute";
                    imgHover.style.margin = "0 auto";
                    imgHover.style.left = "45px";
                    imgHover.style.bottom = "35px";
                    imgHover.style.visibility = "hidden";
                    imgContainer.appendChild(imgHover);

                    imgContainer.addEventListener("mouseover", makeVisible, false);

                    function makeVisible()
                    imgHover.style.visibility = "visible";

                    <div id="container">Hover Over Me</div>





                    Having said that, you should avoid setting inline styles on elements as they are hard to override when needed and they often cause duplication of code. It's much simpler to set up CSS classes ahead of time and just apply/remove those classes as needed with the element.classList API.



                    Also, visibility does affect whether you see an element or not, but even when you don't see it, space is allocated in the UI for it, which isn't always desirable. In most cases, using a display:none to hide an element and then simply removing that instruction to show the element is the better approach.



                    Lastly, while using setAttribute() is certainly valid, you can also configure your elements via their direct properties. Almost all HTML attributes map to a corresponding JavaScript object property. Using these can make the code much simpler.



                    Take a look at an example that puts all this together:






                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');

                    // Just set properties of the element directly:
                    imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

                    // Just add pre-made classes to style the element
                    imgHover.classList.add("hoverImg");
                    imgHover.classList.add("hidden");

                    imgContainer.appendChild(imgHover);
                    imgContainer.addEventListener("mouseover", makeVisible);
                    function makeVisible()
                    imgHover.classList.remove("hidden");;

                    .hidden display:none; /* Used when an element needs to be hidden */

                    /* This will be applied via JS */
                    .hoverImg
                    width:30px;
                    height:23px;
                    position: absolute;
                    margin:0 auto;
                    left:45px;
                    bottom:35px;

                    <div id="container">Hover Over Me</div>








                    share|improve this answer












                    Your code works as it should provided that you set up a valid reference to imgContainer and that you set a valid path to an image for the dynamically created element:






                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');
                    imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
                    imgHover.style.width = "30px";
                    imgHover.style.height = "23px";
                    imgHover.style.position = "absolute";
                    imgHover.style.margin = "0 auto";
                    imgHover.style.left = "45px";
                    imgHover.style.bottom = "35px";
                    imgHover.style.visibility = "hidden";
                    imgContainer.appendChild(imgHover);

                    imgContainer.addEventListener("mouseover", makeVisible, false);

                    function makeVisible()
                    imgHover.style.visibility = "visible";

                    <div id="container">Hover Over Me</div>





                    Having said that, you should avoid setting inline styles on elements as they are hard to override when needed and they often cause duplication of code. It's much simpler to set up CSS classes ahead of time and just apply/remove those classes as needed with the element.classList API.



                    Also, visibility does affect whether you see an element or not, but even when you don't see it, space is allocated in the UI for it, which isn't always desirable. In most cases, using a display:none to hide an element and then simply removing that instruction to show the element is the better approach.



                    Lastly, while using setAttribute() is certainly valid, you can also configure your elements via their direct properties. Almost all HTML attributes map to a corresponding JavaScript object property. Using these can make the code much simpler.



                    Take a look at an example that puts all this together:






                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');

                    // Just set properties of the element directly:
                    imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

                    // Just add pre-made classes to style the element
                    imgHover.classList.add("hoverImg");
                    imgHover.classList.add("hidden");

                    imgContainer.appendChild(imgHover);
                    imgContainer.addEventListener("mouseover", makeVisible);
                    function makeVisible()
                    imgHover.classList.remove("hidden");;

                    .hidden display:none; /* Used when an element needs to be hidden */

                    /* This will be applied via JS */
                    .hoverImg
                    width:30px;
                    height:23px;
                    position: absolute;
                    margin:0 auto;
                    left:45px;
                    bottom:35px;

                    <div id="container">Hover Over Me</div>








                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');
                    imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
                    imgHover.style.width = "30px";
                    imgHover.style.height = "23px";
                    imgHover.style.position = "absolute";
                    imgHover.style.margin = "0 auto";
                    imgHover.style.left = "45px";
                    imgHover.style.bottom = "35px";
                    imgHover.style.visibility = "hidden";
                    imgContainer.appendChild(imgHover);

                    imgContainer.addEventListener("mouseover", makeVisible, false);

                    function makeVisible()
                    imgHover.style.visibility = "visible";

                    <div id="container">Hover Over Me</div>





                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');
                    imgHover.setAttribute("src", "https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png");
                    imgHover.style.width = "30px";
                    imgHover.style.height = "23px";
                    imgHover.style.position = "absolute";
                    imgHover.style.margin = "0 auto";
                    imgHover.style.left = "45px";
                    imgHover.style.bottom = "35px";
                    imgHover.style.visibility = "hidden";
                    imgContainer.appendChild(imgHover);

                    imgContainer.addEventListener("mouseover", makeVisible, false);

                    function makeVisible()
                    imgHover.style.visibility = "visible";

                    <div id="container">Hover Over Me</div>





                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');

                    // Just set properties of the element directly:
                    imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

                    // Just add pre-made classes to style the element
                    imgHover.classList.add("hoverImg");
                    imgHover.classList.add("hidden");

                    imgContainer.appendChild(imgHover);
                    imgContainer.addEventListener("mouseover", makeVisible);
                    function makeVisible()
                    imgHover.classList.remove("hidden");;

                    .hidden display:none; /* Used when an element needs to be hidden */

                    /* This will be applied via JS */
                    .hoverImg
                    width:30px;
                    height:23px;
                    position: absolute;
                    margin:0 auto;
                    left:45px;
                    bottom:35px;

                    <div id="container">Hover Over Me</div>





                    var imgContainer = document.getElementById("container");
                    var imgHover = document.createElement('img');

                    // Just set properties of the element directly:
                    imgHover.src ="https://www.wpclipart.com/signs_symbol/arrows/button_arrows/play_buttons/play_button_gray.png";

                    // Just add pre-made classes to style the element
                    imgHover.classList.add("hoverImg");
                    imgHover.classList.add("hidden");

                    imgContainer.appendChild(imgHover);
                    imgContainer.addEventListener("mouseover", makeVisible);
                    function makeVisible()
                    imgHover.classList.remove("hidden");;

                    .hidden display:none; /* Used when an element needs to be hidden */

                    /* This will be applied via JS */
                    .hoverImg
                    width:30px;
                    height:23px;
                    position: absolute;
                    margin:0 auto;
                    left:45px;
                    bottom:35px;

                    <div id="container">Hover Over Me</div>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 21:04









                    Scott Marcus

                    38.3k51936




                    38.3k51936




















                        up vote
                        -1
                        down vote













                        Here you were appending element like this 
                        imgContainer.appendChild(imgHover);
                        So reference for imgHover element in dom will get
                        change. Fetch that element once again inside
                        makeVisible() function.
                        document.querySelector("img") // use your appropriate.





                        share|improve this answer
























                          up vote
                          -1
                          down vote













                          Here you were appending element like this 
                          imgContainer.appendChild(imgHover);
                          So reference for imgHover element in dom will get
                          change. Fetch that element once again inside
                          makeVisible() function.
                          document.querySelector("img") // use your appropriate.





                          share|improve this answer






















                            up vote
                            -1
                            down vote










                            up vote
                            -1
                            down vote









                            Here you were appending element like this 
                            imgContainer.appendChild(imgHover);
                            So reference for imgHover element in dom will get
                            change. Fetch that element once again inside
                            makeVisible() function.
                            document.querySelector("img") // use your appropriate.





                            share|improve this answer












                            Here you were appending element like this 
                            imgContainer.appendChild(imgHover);
                            So reference for imgHover element in dom will get
                            change. Fetch that element once again inside
                            makeVisible() function.
                            document.querySelector("img") // use your appropriate.






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 20:41









                            Nattamai Jawaharlal Manikandan

                            2068




                            2068



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252652%2fhow-to-change-visibility-css-property-using-dom-when-mouseover%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto