1

I am stuck while creating List (Ul li),infact this is a menu pupolating at runtime.

Like

.Computers items
.Keyboard
.Cd rom
.office Equipment
.item 1
.item 2
.item 3
.item 4
.item 5

problem is, when i tryied to add new

  • at run time (after click on any category for example Computers)then it adds new items in same (parent) anyone can help me out. thanks

    I am using this code

    $("#cssmenu ul").append('<li class="active has-sub" id="'+ id.toString()+'" onclick="My_Func($(this).html(),this.id)"><a href="#"><span>'+txt +'</span></a>');
    
  • 3
    • wrap it in a ul and append to that ul. Commented Nov 20, 2014 at 9:33
    • abih: i am having problem to send ID on click event and then track the element to fill that with new values Commented Nov 20, 2014 at 9:38
    • Added as the answer. Commented Nov 20, 2014 at 10:01

    1 Answer 1

    1

    You should first wrap the contents of your first-level li (i.e. the ones you want to be click-able and load data) in a. This will save you from the frustration of multi-level li targeting. Also give a unique id to your li so as to load corresponding data.

    Then, bind to the click event on those a and create a ul. Load data into li and append them to this ul. Once done, append this ul into the currently clicked li.

    This snippet will make it clear to you:

    For the purpose of this demo, we loading data from an object dictionary which contains the contents of each category as an array.

    /* For the purpose of this demo, data is stored in this object */
    var items = {
    	"computers": ['Computer 1', 'Computer 2', 'Computer 3'],
    	"equipment": ['Eq 1', 'Eq 2'],
    	"peripherals": ['CD', 'Printer', 'etc']
    };
    
    /* Target the a which are direct children of li which are direct children of list ul  */
    $("ul#list > li > a").on("click", function(e) {
      
        // When a is clicked, we get the id of parent li
        var id = $(this).parent().attr("id");
      
        // Create a ul
        var $wrap = $("<ul />");
      
        // Loop thru data, or loaded thru other means
        for (var idx in items[id]) {
          
            // Create li from data and append to ul
            $wrap.append("<li>" + items[id][idx] + "</li>");
        }
      
        // append this ul to the current li
        $(this).parent().append($wrap);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul id="list">
        <li id="computers"><a href="#">Computers</a></li>
        <li id="equipment"><a href="#">Equipment</a></li>
        <li id="peripherals"><a href="#">Peripherals</a></li>
        <li>Others</li>
    </ul>

    Sign up to request clarification or add additional context in comments.

    4 Comments

    Thanks Abih! I am trying to run this code at my side but its not loading details i dont know y but i am trying Thanks Again.....
    How are you loading your data? From where is that coming from?
    it from SQL server, now am running same code and onclick its not riggers the event and no loading performs...
    In that case plug this code in, when your ajax call returns.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.