2

Small part of my html code :

<div class="text-center">

        <div class="btn-group">
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                Platforms <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu" id = "buttonsPlace">
            </ul>

        </div>

In my js file :

for (i = 0; i < platformList.length; i++) {
        var li =  $("<li/>" , { id : "plat"+i,class : "dropdown" text : platformList[i]  } )
        //var text = document.createTextNode(platformList[i]);
        //li.appendChild(text);
        //btn.data("platform", platformList[i] );
         $("#buttonsPlace").append(li);
        console.log("hiding");
        $("#plat" + i).hide();
    }

However the menu is appearing but the menu items are not. where am i going wrong

0

2 Answers 2

3

Try This

$(function() {
    var change = function( txt ) {
        $("#ID").append( '<li>' + txt + '</li>' );
    };
    change("this is the first change");
    change("this is the second change");
});

Demo

For Li Click

$("ul").on('click', 'li', function () {
   var id = this.id;
   //play with the id
});
Sign up to request clarification or add additional context in comments.

4 Comments

yeah but how do i add the onclick function to these items in the dropdown list now?
even if i add the id like this <li id = "plat"+i> , how do i add click on function to these so that images are displayed on clicking them
what you want to do exactly?
i need to display images whenever i click on any of the items in the drop down menu. so for that i have attached IDs with all <li> items by doing <li id = "plat"+i> in the loop where i is variable. So that i can do $("#plat"+i).click() later on. but this isnt working. the drop down list is showing but the click function is not linked to the items in the menu
1
$(function(){

    $('.dropdown-toggle').click(function(){

        var countRows = $('ul.dropdown-menu li').size();
        $('.dropdown-menu').append('<li>Row '+countRows+'</li>');
        countRows++;
    });

});

Here is the jsfiddle for you http://jsfiddle.net/alexchizhov/ncgXK/

$('#drowdown-link1').click(function(e){
   e.preventDefault();
   window.location.href = 'http://example.com';
});

Here is another jsfiddle for you http://jsfiddle.net/alexchizhov/ncgXK/4/

2 Comments

yeah but how do i add the onclick function to these items in the dropdown list now? even if i add the id like this <li id = "plat"+i> , how do i add click on function to these so that images are displayed on clicking them

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.