• New...">
    Clever Geek Handbook

    How to create a dynamic drop?

    Here is my HTML code.

    <dl id="sample" class="dropdown">
    <dt><a href="#"></a></dt>
    <dd>
        <ul>
            <li><a href="#">News</a></li>
            <li><a href="#">Jobs</a></li>
            <li><a href="#">Community</a></li>
            <li><a href="#">Events</a></li>
            <li><a href="#">Advisory</a></li>
            <li><a href="#">People</a></li>
            <li><a href="#">Universities</a></li>
            <li><a href="#">HR Intellegence</a></li>
            <li><a href="#">Companies</a></li>
            <li><a href="#">Inbox</a></li>
            <li><a href="#">Notifications</a></li>
        </ul>
    </dd>
    
          

    And this is js

    $(document).ready(function() {
     $(".dropdown img.flag").addClass("flagvisibility");
    
     $(".dropdown dt a").click(function() {
       $(".dropdown dd ul").toggle();
     });
    
     $(".dropdown dd ul li a").click(function() {
       var text = $(this).html();
       $(".dropdown dt a span").html(text);
       $(".dropdown dd ul").hide();
     });
    
     function getSelectedValue(id) {
       return $("#" + id).find("dt a span.value").html();
     }
    
     $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass("dropdown"))
        $(".dropdown dd ul").hide();
      });
    
      $("#flagSwitcher").click(function() {
         $(".dropdown img.flag").toggleClass("flagvisibility");
      });
    });
    
          

    This is the code for the dropdown that is clicked to fill. I want to repeat it 100 times in my webpage and I don't want to copy the code and change the class name or id for every entry. I remember there is a function like .this' for this, but I don't know how to use it. Also I have some dynamic posts that contain this list so its JS cannot be repeated in every new post.

    Please suggest changes to this code that will allow me to use the ".dropdown" class with every post without repeating the JS code, and it only opens the dropdown that opens to open.

    +3
    javascript jquery html


    Mian Kamran 07 June At 7:05 am
    source to share


    2 answers


    I did something for you:



    <!DOCTYPE html>
    <html>
    <head>
        <style>
        html {
            font-family: sans-serif;
        }
        .dropdown {
            display: inline-block;
        }
        .dropdown dd {
            display: none;
            box-shadow: 0px 0px 2px 0px #555;
            width: 180px;
            position: absolute;
            margin-left: 1px;
            margin-top: 1px;
            background-color: #FFF;
        }
        .dropdown ul {
            list-style: none;
            padding-left: 0px;
        }
        dt a {
            display: inline-block;
            background-color: #1b8be0;
            padding: 10px;
            color: #FFF;
            text-decoration: none;
        }
        .dropdown li a{
            text-decoration: none;
            color: #333;
        }
        .dropdown li {
            padding: 6px;
            cursor: pointer;
        }
        .dropdown li:hover{
            background-color: #EEE;
            padding: 6px;
        }
        </style>
    </head>
    <body>
    <dl id="sample" class="dropdown">
        <dt><a href="#">Menu</a></dt>
        <dd>
            <ul>
                <li><a href="#">News</a></li>
                <li><a href="#">Jobs</a></li>
                <li><a href="#">Community</a></li>
            </ul>
        </dd>
    </dl>
    <dl id="sample-2" class="dropdown">
        <dt><a href="#">Menu 2</a></dt>
        <dd>
            <ul>
                <li><a href="#">More News</a></li>
                <li><a href="#">More Jobs</a></li>
                <li><a href="#">More Community</a></li>
            </ul>
        </dd>
    </dl>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
    $(".dropdown dt a").click(function(e){
        $(this).parent().next().fadeToggle("fast");
    });
    $(".dropdown dt a").blur(function(e){
        if ($(e.target).not(".dropdown ul")){
            $(".dropdown dd").fadeOut();
        }
    });
    </script>
    </body>
    </html>
    
          

    +1


    uz4ir 07 June 15 at 11:18
    source to share


    Better for you to use a front-end framework like Twitter Bootstrap or Zurb Foundation.



    0


    uz4ir 07 June 15 at 8:43
    source to share






    More articles:

    • Angular does not evaluate expression in form action - javascript
    • Jax-RS overloading methods / execution paths - rest
    • OrientDB: how to update a column using select query - sql
    • Threaded sort is slower than non-threaded sort - java
    • How does JAX-RS match two or more compatible @Path expressions? - jax-rs
    • how to use select parameters in where is a subquery clause in oracle? - oracle11g
    • How to store a string property with a foreign key reference in a database - c #
    • Elasticsearch or match query - php
    • What is the correct way to bind a child to a parent shared model? - angular
    • Matlab: creating symfun array - arrays

    All Articles

    Daily Blog | 2020

    Green Geek Media (GGM)
    1298 Despard Street
    GA 30344 East Point, USA
    404-763-3837