Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 4th, 2008, 12:49 PM
Registered User
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete option(s) based on substring of value

Howdy,
I'm trying to delete options based on a substring of their value but my code will only delete the first option and no others even though they match the substring.

The list in the select box will be dynamic so I can't always say "delete the first three options." I can control the value of the prog2 box so I'm making the first two characters be something I can use to determine what to delete.

Any ideas what I'm missing in getting the function to match to the other AO options so they can be deleted? Thanks! Lori
HTML:
Code:
<td><label for="prog2" name="prog1" id="prog1">Program Code:</label></br>
  <select name="prog2" id="prog2" multiple size="5">
  <option value="ALL" selected="selected">ALL</option>
  <option value="A0101">A0 - 101 - Regular Term Instruction</option>
  <option value="A0102">A0 - 102 - Summer Term Instruction</option>
  <option value="A0103">A0 - 103 - Extension Instruction/Non-Cred</option>
  <option value="D0101">D0 - 101 - Regular Term Instruction</option>
  <option value="D0110">D0 - 110 - Organized Research</option>
  <option value="D0121">D0 - 121 - Administration</option>
  <option value="M0990">M0 - 990 - Multi-activity</option>
  <option value="L0110">L0 - 110 - Organized Research</option>
  <option value="L0152">L0 - 152 - General Academic Support</option>
  <option value="L0230">L0 - 230 - Student Financial Aid</option>
  <option value="L0990">L0 - 990 - Multi-activity</option>
    </select></td>
Javascript:
Code:
function updateProgList()
{
var progElement = document.getElementsByTagName('select')['prog2'];

// AO is hard-coded for now; later it will be read in from another 
// select box to determine which values to delete from this box.

for ( var i = 0; i < progElement.length; i++ )
    { if ( progElement[i].value.substring(0,2) == 'A0' )
         { progElement.removeChild(progElement[i]);
         }
    }
}
 
Old February 5th, 2008, 01:43 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Lori713!!
Traverse backword
for ( var i = progElement.length-1; i <=0; i++ )
please check the syntax

Cheers :)

vinod
 
Old February 5th, 2008, 12:57 PM
Registered User
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vinod,

Thank you for the reply! I had never thought about approaching it from that direction. I will give that a shot.

:-)

Lori
 
Old February 7th, 2008, 09:00 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default


Hii Lori713!!
I am sorry, i was forget to decrement it .i.e
i--
following code is working fine.
<script>
function removeItems()
{
var progElement = document.getElementsByTagName('select')['prog2'];


for ( var i = progElement.length-1;i>=0; i--)
    {
          if ( progElement[i].value.substring(0,2) == 'A0' )
         {
         progElement.removeChild(progElement[i]);
         }
    }


}
</script>
<select name="prog2" id="prog2" multiple size="15">
  <option value="ALL" selected="selected">ALL</option>
  <option value="A0101">A0 - 101 - Regular Term Instruction</option>
  <option value="A0102">A0 - 102 - Summer Term Instruction</option>
  <option value="A0103">A0 - 103 - Extension Instruction/Non-Cred</option>
  <option value="D0101">D0 - 101 - Regular Term Instruction</option>
  <option value="D0110">D0 - 110 - Organized Research</option>
  <option value="D0121">D0 - 121 - Administration</option>
  <option value="M0990">M0 - 990 - Multi-activity</option>
  <option value="L0110">L0 - 110 - Organized Research</option>
  <option value="L0152">L0 - 152 - General Academic Support</option>
  <option value="L0230">L0 - 230 - Student Financial Aid</option>
  <option value="L0990">L0 - 990 - Multi-activity</option>
    </select>
<input type=button onclick="removeItems()" name="mybtn" value="removeA0Items">

Hope this will help you

Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Check Boxes/Option buttons/Option Group hewstone999 Access VBA 1 March 14th, 2008 07:25 AM
substring in c# sudhirbharti C# 1 February 21st, 2008 01:29 PM
SubString prasanta2expert Access VBA 1 November 17th, 2006 10:04 AM
sorting based on substring-before Kabe XSLT 1 October 14th, 2005 09:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.