Results 1 to 5 of 5

Thread: Hide a DIV when page renders

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Hide a DIV when page renders

    I'm trying to do some minor changes on a web application but the source code is missing. I think I can modify the ASPX page for minor visual changes and that's what I'm trying to do but not really adept to HMTL scripting.

    Page has several buttons. When a button is pressed, a DIV with labels inside is displayed. One button incorrectly shows 2 DIVs. I want to hide one
    when page renders. I cannot use the OnClick to call a function because it is already used in Code Behind.

    How can I hide/intercept page render for one of the DIV when both DIVs are visible?

    TIA

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: Hide a DIV when page renders

    It looks like I can use this script below. It hides DIV2 but I want to use it only when DIV1 and DIV2 is visible on the screen. How do I check for that condition?

    <script>
    window.onload = function(){
    document.getElementById("myDIV2").style.display='none';
    };
    </script>

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,206

    Re: Hide a DIV when page renders

    Set the style of the one div to hidden right from the start... you're over thinking it. You don't need to do it from script... just set it's style to hidden right from the start.

    Then when you want to show it, remove the hidden style. It's as simple as that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: Hide a DIV when page renders

    Looks like this worked when I tested. I'm open to other inputs.

    Code:
    <script>
    window.onload = function(){
    
    	var x = document.getElementById("myDIV1");
    	var y = document.getElementById("myDIV2");
    	
    	if ((x.visibility !== "hidden") && (y.visibility !== "hidden")){
      document.getElementById("myDIV1").style.display='none'};
    };
    </script>
    Last edited by LUCKY_PRINCE; Jan 26th, 2021 at 03:07 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: Hide a DIV when page renders

    Quote Originally Posted by techgnome View Post
    Set the style of the one div to hidden right from the start... you're over thinking it. You don't need to do it from script... just set it's style to hidden right from the start.

    Then when you want to show it, remove the hidden style. It's as simple as that.

    -tg
    Yeah all DIVs hidden from the start.

    click BUTTON 1 displays DIV1 and hides all other DIVs
    click BUTTON 2 displays DIV2 with DIV1 (DIV1 should not be showing)
    click BUTTON 3 displays DIV3 and hides all other DIVs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width