skip to Main Content

How can I hide a div by ID / Class using a parameter in the URL?

For example site.com/?divID=hide

Or disable a specific CSS to act another. But it had to be through the URL: /

2

Answers


  1. var getParams = function() {
        var url = window.location
        var params = {};
        var parser = document.createElement('a');
        parser.href = url;
        var query = parser.search.substring(1);
        var vars = query.split('&');
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=');
            params[pair[0]] = decodeURIComponent(pair[1]);
        }
        return params
    }
    
    var urlParams= getParams();
    

    You will get URL params in "urlParams" object and use that "urlParams[divID]" to get the details of URL params and apply CSS using jQuery/js .

    Here is the syntax of jQuery to apply CSS

    https://www.w3schools.com/jquery/jquery_css.asp

    Here is the syntax of js to apply CSS

    https://www.w3schools.com/js/js_htmldom_css.asp

    Login or Signup to reply.
  2. In HTML, you can hide any element you want by giving it the hidden parameter.

        <div class="container" hidden> *content* </div>
    

    This will hide the div and it’s content.
    If you wanted to do it using PHP, you could do the following: (assuming your URL is site.com?hide)

    <?php
    $hide = isset($_GET['hide']); //this returns a boolean value
    ?>
    
    <body>
        <div <?= $hide ? "hidden":""?> > *content* </div>
    </body>
    
    

    What <?= $hide ? "hidden":""?> does is basically, if the GET parameter hide is set, will print in your HTML (inside your div tag) the word ‘hidden’, which will make your div invisible.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search