vinothini
vinothini

Reputation: 2604

Adding and removing li by Javascript

I am having left side pane and right side pane. I want to deal with two scenarios

  1. When I click the list(li) on right side that want add on left side(`initially left side pane is empty) and remove from right side.
  2. Reverse part of first one. When i click the added li in left side that need to be remove from the left side and added to the right side.

I did the first scenario like below

<div class="left_pane">
  <div class="content_left_pane">
    <ul data-role="listview" id="left_side_ul">
    </ul>
   </div>
 </div>

<div class="right_pane">
 <ul data-role="listview" id="right_side_ul">   
  <li class="showDetails" id="account-1" onclick="selected_destination($(this).attr('id'))">
    <div class="ui-grid-b">
       <div class="ui-block-a"><div>[Facility name] A</div><div>[Account]</div></div>
       <div class="ui-block-b"><div>[Address Line 1]</div><div>[Address Line 2]</div><div>[City],[Statte],[Zip code]</div></div>
       <div class="ui-block-c"><span class="ui-li-count" style="right: 30%; font-size: 15px;">12</span></div>
     </div>             
   </li>    
 </ul>
</div>



<script>
function selected_destination(id){   
      $("#right_side_ul li#"+id+"").clone().prependTo("div.content_left_pane ul#left_side_ul");
      $("#right_side_ul li#"+id+"").remove();
    }

  </script>

I don't have any clue to do the second part. Because in the first part i am sending the current li id in onclick function. But in the reverse part, in the left pane i am having ul only so i don't know how to do the second scenario.

Upvotes: 0

Views: 224

Answers (1)

aliona
aliona

Reputation: 447

I suggest you to not register onclick handler directly in html code. As you are using jQuery, this scenario should work for you:

HTML:

<div class="left_pane">
  <div class="content_left_pane">
    <ul data-role="listview" id="left_side_ul">
    </ul>
   </div>
 </div>

<div class="right_pane">
 <ul data-role="listview" id="right_side_ul">   
  <li class="showDetails" id="account-1">
    <div class="ui-grid-b">
       <div class="ui-block-a"><div>[Facility name] A</div><div>[Account]</div></div>
       <div class="ui-block-b"><div>[Address Line 1]</div><div>[Address Line 2]</div><div>[City],[Statte],[Zip code]</div></div>
       <div class="ui-block-c"><span class="ui-li-count" style="right: 30%; font-size: 15px;">12</span></div>
     </div>             
   </li>    
 </ul>
</div>

JavaScript:

$(document).ready(function(){
    $('#right_side_ul li').live('click', function(){
        $(this).appendTo('#left_side_ul');
    });

    $('#left_side_ul li').live('click', function(){
        $(this).appendTo('#right_side_ul');
    });
});​

Please note, that I'm using live() here because LI elements are dinamically added to the UL.

As you can see, javascript code for both right and left panes are almost the same, so you may optimize my solution by combining them into one

Upvotes: 2

Related Questions