Avatar of MiamiDolphins
MiamiDolphins asked on

How to compare the values in two list boxes

Hey there,
im using javascript and i would like to compare the values in two listboxes (list box A, and list box B)


when the compare all the items in listbox B which also appear in list box A need to be SELECTED in listbox A.

can anyone help  me to do this - (using asp.net c# and javascript - new to both)
JavaScriptASP.NETC#

Avatar of undefined
Last Comment
HonorGod

8/22/2022 - Mon
divinewind80

Please see this PAQ for some ideas on looping through a Listbox with Javascript:  https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21509625.html

Can you show the code you have already and what problems you are having?  Also, please explain your second line a little better.  Are you wanting to search through confirm all items in List box B are selected in List box A?
HonorGod

Please define the comparison test(s) to be made.

For example, if item #1 on List A has the same text as item #12 on List B, but the "value" attributes don't match, is that "close enough"?
ASKER
MiamiDolphins

the matches will be identical matches (value matches)

so the value and text attributes will be the same in both cases as they are sourced from the same database tables.


sadly im not at work so i cant supply any sample codes at the moment :-(


but just imagine two listboxes (asp listboxes - selectmode - multiple) and they both have items

i just want to be able to loop through listbox 2 on an event (i.e button click) and then at the end event i want to be able to see which values matched in listbox 1 because they would be selected
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
HonorGod

Can a value from list A exist multiple times in list B?
ASKER
MiamiDolphins

no it can only exist once


HonorGod

something like this?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>compare selection lists</title>
<script type='text/javascript'>
  function field( id ) {
    var ele = document.getElementById( id );
    if ( !ele ) {
      alert( 'Specified document element not found.  id="' + id + '"' );
    }
    return ele;
  }
 
  function selCheck( sel ) {
    var result = ( sel.nodeName == 'SELECT' )
    var id = sel.getAttribute( 'id' );
    if ( !result ) {
      alert( 'document element is not a SELECT.  id="' + id + '"' );
    }
    return result;
  }
 
  function compare( id1, id2 ) {
    var sel1 = field( id1 );
    var sel2 = field( id2 );
    var matches = 0;
    if ( sel1 && sel2 && selCheck( sel1 ) && selCheck( sel2 ) ) {
      for ( var j = 0; j < sel2.options.length; j++ ) {
        sel2.options[ j ].selected = false;
      }
      for ( var i = 0; i < sel1.options.length; i++ ) {
        var opt = sel1.options[ i ];
//      alert( 'opt: ' + opt.value );
        for ( var j = 0; j < sel2.options.length; j++ ) {
          if ( sel2.options[ j ].value == opt.value ) {
            sel2.options[ j ].selected = true;
            matches++;
          }
        }
      }
      alert( 'Number of matching entries: ' + matches );
    }
  }
</script>
</head>
<body>
  <table border='1'>
    <thead>
      <th>List A</th>
      <th>List B</th>
    </thead>
    <tbody>
      <tr>
        <td>
          <select id='A'>
            <option>Now</option>
            <option>is</option>
            <option>the</option>
            <option>time</option>
            <option>for</option>
            <option>all</option>
            <option>good</option>
            <option>men</option>
            <option>to</option>
            <option>jump</option>
            <option>dog</option>
          </select>
        </td>
        <td>
          <select id='B' multiple>
            <option>The</option>
            <option>quick</option>
            <option>brown</option>
            <option>fox</option>
            <option>jumps</option>
            <option>over</option>
            <option>the</option>
            <option>lazy</option>
            <option>dog</option>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan='2' align='center'>
          <input type='button' value='compare' onclick='compare("A","B")'>
        </td>
      </tr>
    </tbody>
  </table>
</body>
</html>

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
HonorGod

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
MiamiDolphins

ill try and test it out later tonight

thanks buddy will let you know the results
ASKER
MiamiDolphins

fantastic - great stuff
HonorGod

Great.  I'm glad to have been able to help
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes