www.webdeveloper.com
Results 1 to 2 of 2

Thread: Javascript move to top of list button

  1. #1
    Join Date
    Jul 2009
    Posts
    1

    Javascript move to top of list button

    Help! I am a noobie with JavaScript and I am trying to create a button that can move a selected item to the top of a list of items. So if there is a list of 20 items, and they highlight the bottom item in the list, they can click a button to automatically move the last item (along with others if selected) to the top. Here is what I have so far but nothing it is not working, when I click the button, it doesn't do anything.


    SearchFieldsWindow.prototype.cmdMoveTop_OnClick =
    function()
    {
    var searchFields = this.__searchFields;
    var items = this.__lstSelected.get_Items();
    var selectedIndices = this.__lstSelected.get_SelectedIndices();
    var count = selectedIndices.get_Count()

    var newArray = [];
    newArray[0] = selectedIndices;
    for ( var i = 1; i < items.length + 1; i++ )
    {
    newArray[i] = items[i - 1];
    items = newArray;

    var selectedIndex = selectedIndices.get_Item(i);
    // Don't allow when first item selected
    if (selectedIndex == 0)
    break;

    var uniqueID = items.get_ItemValue(selectedIndex);
    var label = items.get_ItemLabel(selectedIndex);
    searchFields.MoveUp(uniqueID);

    items.RemoveAt(selectedIndex);
    items.Insert(selectedIndex - 1, uniqueID, label);
    this.__lstSelected.SetSelected(selectedIndex - 1, true);
    this.__lstSelected.SetSelected(selectedIndex, false);
    }
    };

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    Can you use anything like this?
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <
    HTML>
    <
    HEAD><TITLE>Th5-55pm47</TITLE>
    <
    META http-equiv=Content-Type content="text/html; charset=UTF-8">
    <
    STYLE type=text/CSS>

    </
    STYLE>
    <
    SCRIPT type="text/javascript">

    var 
    arr=new Array('one','two','three','four','five','six')

    function 
    topIt(x){    x=Number(x);
        
    g=arr[x]; arr.splice(x,1);
         
    arr.splice(0,0,g);selc(); 

    }
    function 
    selc(){ var s='<SELECT id="slct" size='+arr.length+'>';
        for(
    i=0;i<arr.length;i++){s+='<option value="'+i+'">'+arr[i]+''}
        
    document.getElementById('sel').innerHTML=s+'</SELECT>'
    }
    </SCRIPT>
    <META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
    <BODY scroll="auto" onload="selc()">
        <span id="sel"></span>
        <input type=button onclick="topIt(document.getElementById('slct').value)" value="move to top">
    </BODY></HTML> 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles