ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Jquery select Add or Remove
    Front-End/jQuery 2012. 11. 14. 10:31

    JQuery makes it very easy to select specific elements and their attributes in your web page HTML.

    In the following tutorial I will show you how to use just a few lines of jQuery to create an add/remove select list for your website form.

    The Form HTML

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <form>
      <fieldset>
     
        <select name="selectfrom" id="select-from" multiple size="5">
          <option value="1">Item 1</option>
          <option value="2">Item 2</option>
          <option value="3">Item 3</option>
          <option value="4">Item 4</option>
        </select>
     
        <a href="JavaScript:void(0);" id="btn-add">Add &raquo;</a>
        <a href="JavaScript:void(0);" id="btn-remove">&laquo; Remove</a>
     
        <select name="selectto" id="select-to" multiple size="5">
          <option value="5">Item 5</option>
          <option value="6">Item 6</option>
          <option value="7">Item 7</option>
        </select>
     
      </fieldset>
    </form>

    So here we have just 2 select lists with sample data – the top one listing the items “from”. There are also 2 anchor tags, which provide the “add” and “remove” links.

    The JQuery Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $(document).ready(function() {
     
        $('#btn-add').click(function(){
            $('#select-from option:selected').each( function() {
                    $('#select-to').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
                $(this).remove();
            });
        });
        $('#btn-remove').click(function(){
            $('#select-to option:selected').each( function() {
                $('#select-from').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
                $(this).remove();
            });
        });
     
    });

    Basically what we do here is that when either of the “add” or “remove” links are clicked jQuery will get the values for all of the relevant options that are selected, recreate each option item HTML, insert it into the opposite select list and finally remove these items from the original list.


    Check Out The Jquery Add & Remove Select List Demo


    'Front-End > jQuery' 카테고리의 다른 글

    웹앱 롱터치 clipboard 현상 막기  (0) 2013.08.20
    Jquery js file Error 대처법  (0) 2013.07.16
    Div Loading 이미지 영역 Dimm  (0) 2013.07.04
    날짜값 + - 현재 날짜 가져오기  (0) 2013.07.04
    JSON object 복사 / clone  (0) 2013.05.24
    Jquery Selector 예시  (0) 2012.11.08
    제이쿼리 체크박스  (0) 2012.11.06
    날짜 시간값 계산  (0) 2012.07.24
    Jquery 아코디언 형식  (0) 2012.06.27
    CSS position 속성  (0) 2012.06.27

    댓글

COPYRIGHT 2010 EpoNg. ALL RIGHTS RESERVED.