Search code examples
jqueryclickappendto

Append ID to some element from other element click action


i have a little problem with jQuery. I want to append ID for element from other element after that i click on it... :) So, if i click some list element this element id will go to another, can you hel me? This is my code...

        $('#main_menu li').click(function(){
            (this.id).appendTo('.main_content ul')
        })

Solution

  • $('#main_menu li').click(function(){
      $('.main_content ul').attr('id', this.id);
    });
    

    Here this.id will get the clicked element id and set that id to .main_content ul.

    But this will make id duplication, which is not allowed.

    Instead of id you can make class:

    $('#main_menu li').click(function(){
      $('.main_content ul').attr('class', this.id);
    });