SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    should be simple but?

    I am trying to create a simple function to select everything in a listbox. Here is what I came up with

    Code:
    function highlightmembers()
    {
    var destList = window.document.forms[0].elements['destList[]'];
    for (var i =0; i < destList.options.Length; i++)
    {
    if(destList.options[i].value !="0")
    {
    destList.options[i].selected = true;
    }
    } 
    }
    
    Code:
    <input type="button" Name="Test" value="test" onclick="javascript:highlightmembers()">
    
    When I press the button nothing happens, no errors are reported or anything. Anyone have an idea why?
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was getting errors before with your example. But I made some tweaks and now I get no errors at all but at the same time, not getting multiple items selected either. ISn't there another kind of form field that does multiple selections??
    Code:
    <head>
    <script type="text/javascript">
    function highlightmembers() { var destList = 
    
    window.document.forms[0].elements['destList[]']; for (var i =0; i < 
    
    destList.options.Length; i++) { if(destList.options[i].value !="0") { 
    
    destList.options[i].selected = true; } } }
    </script>
    </head>
    <body>
    <form action="self.php" method="post">
    <select name="destList[]">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <input type="button" Name="Test" value="test" 
    
    onclick="javascript:highlightmembers();">
    </form>
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sketch
    ISn't there another kind of form field that does multiple selections??
    Yup just use the multiple attribute.
    Code:
    <select multiple="multiple">
    As for your question archigamer, I can't be of any help.

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    <select type="multiple"...>

    archigamer: can I see some of the html code?

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well to get the whole picture you have to look at my other post on acessing forms with [] in them. so here is the code (not mine, took off javascript.com and modified to my needs)

    Code:
    <script language="JavaScript">
    <!--
    // Add the selected items in the parent by calling method of parent
    function highlightmembers()
    {
    var destList = window.document.forms[0].elements['destList[]'];
    for (var i =0; i < destList.options.Length; i++)
    {
    if(destList.options[i].value !="0")
    {
    destList.options[i].selected = true;
    }
    } 
    }
    function addSelectedItemsToParent() {
    self.opener.addToParentList(window.document.forms[0].elements['destList[]']);
    window.close();
    }
    // Fill the selcted item list with the items already present in parent.
    function fillInitialDestList() {
    var destList = window.document.forms[0].elements['destList[]']; 
    var srcList = self.opener.window.document.forms[0].parentList;
    for (var count = destList.options.length -1; count >=0; count--) {
    destList.options[count]= null;
    }
    for(var i =0; i < srcList.options.length; i++) { 
    if (srcList.options[i]!= null)
    opt = new Option(srcList.options[i].text, srcList.options[i].value);
    }
    }
    // Add the selected items from the source to destination list
    function addSrcToDestList() {
    destList = window.document.forms[0].elements['destList[]'];
    srcList = window.document.forms[0].srcList; 
    var len = destList.length;
    for(var i =0; i < srcList.length; i++) {
    if ((srcList.options[i]!= null)&&(srcList.options[i].selected)) {
    //Check if this value already exist in the destList or not
    //if not then add it otherwise do not add it.
    var found = false;
    for(var count =0; count < len; count++) {
    if (destList.options[count]!= null) {
    if (srcList.options[i].text == destList.options[count].text) {
    found = true;
    break;
    }
    }
    }
    if (found != true) {
    opt = new Option(srcList.options[i].text, srcList.options[i].value);
    //opt.selected = true;
    destList.options[len]= opt;
    len++;
    }
    }
    }
    }
    // Deletes from the destination list.
    function deleteFromDestList() {
    var destList = window.document.forms[0].elements['destList[]'];
    var len = destList.options.length;
    for(var i =(len-1); i >=0; i--) {
    if ((destList.options[i]!= null)&&(destList.options[i].selected == true)) {
    destList.options[i]= null;
    }
    }
    }
    // End -->
    
    then I have some PHP code in here. php is just to populate the selects and to help maintain state.

    Code:
    <form action="<?= $PHP_SELF ?>" method="post" name="theForm">
    <tdwidth="40%">
    <select size="30" name="srcList" multiple>
    <OPTION value="0" SELECTED>------------</OPTION>
    <?php
    $pInfo_query = tep_db_query('select p.products_id, pd.products_name from products p right join products_description pd on p.products_id = pd.products_id where pd.language_id = '. $languages_id .' order by pd.products_name');
    while($pInfo = tep_db_fetch_array($pInfo_query))
    {
    echo("\n" . '<option value="'. $pInfo['products_id'] .'">'. $pInfo['products_name'] .'</option>');
    }
    ?>
    </select><br><br>
    </td>
    <tdwidth="20%">
    <input type="button" value=">>" onclick="javascript:addSrcToDestList()"><br>
    <input type="button" value="<<" onclick="javascript:deleteFromDestList()">
    </td>
    <tdwidth="40%">
    <select size="30" name="destList[]" multiple>
    <OPTION value="0" SELECTED>------------</OPTION>
    <?php
    if(sizeof($HTTP_SESSION_VARS['selected_items']) >= 1)
    {
    foreach($HTTP_SESSION_VARS['selected_items'] as $var)
    {
    $sql = 'select p.products_id, pd.products_name from products p right join products_description pd on p.products_id = pd.products_id where pd.language_id = '. $languages_id;
    $sql .= ' and p.products_id =' . $var;
    $selectedItems_query = tep_db_query($sql);
    while($selectedItems = tep_db_fetch_array($selectedItems_query))
    {
    echo("\n" . '<option value="'. $selectedItems['products_id'] .'" SELECTED>'. $selectedItems['products_name'] .'</option>');
    }
    }
    }
    
    ?>
    </select>
    
    then I got this html at the bottom for the buttons

    Code:
    <tdcolspan="2"align="left">
    <input type="submit" Name="AddRow" value="Add Row">
    <input type="button" Name="Test" value="test" onclick="javascript:highlightmembers()">
    </td>
    <tdcolspan="2"align="right">
    <input type="submit" Name="AddOptions" value="Submit Options">

    Web Finesse Studios
    Professional, business oriented web hosting and development.

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Sorry, I asked for all that info (though it did turn out to be somewhat helpful); anyway, "Length" != "length"

    Code:
    function highlightmembers()
    {
       var destList = window.document.forms[0].elements['destList[]'];
       for (var i =0; i < destList.options.length; i++)
    		destList.options[i].selected = (destList.options[i].value !="0") ? true :  false;
    }
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  7. #7
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks that did the trick.
    Web Finesse Studios
    Professional, business oriented web hosting and development.


Bookmarks

Posting Permissions

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