javascript: looping thru elements to find duplicates

Discussion in 'Java' started by da_rod_father, Mar 16, 2006.

  1. Hi all,

    I am entering text into an input field in my jsp, i want to check all
    other input values to see if there is a duplicate. I am using the
    getElementsByName method to do this because it returns a collection of
    values specified by the name. My problem is when i enter in a new value
    and check all other values, it always returns true. I want to compare
    all element values with the new value I just entered. I am looking for
    direction.

    Can anyone help point me in the right direction?
     
    da_rod_father, Mar 16, 2006
    #1
    1. Advertising

  2. da_rod_father

    Hal Rosser Guest

    "da_rod_father" <> wrote in message
    news:...
    > Hi all,
    >
    > I am entering text into an input field in my jsp, i want to check all
    > other input values to see if there is a duplicate. I am using the
    > getElementsByName method to do this because it returns a collection of
    > values specified by the name. My problem is when i enter in a new value
    > and check all other values, it always returns true. I want to compare
    > all element values with the new value I just entered. I am looking for
    > direction.
    >
    > Can anyone help point me in the right direction?
    >


    Boil your code down to a basic page that demonstrates the problem - and post
    it here.
    (What type of input fileds are you working with that have several with the
    same name ? - ?Radio Buttons?)
    usually, in javascript, you can access the field directly as :
    document.formName.ElementName.value .
    2ยข
    HTH
     
    Hal Rosser, Mar 16, 2006
    #2
    1. Advertising

  3. The type of input fields are text and the reason that they have the
    same name is that I am dynamically building the rows that the input
    fields are in. They have the same name but different values. Here is
    a snippet of code that I am working with. I only want to check for
    duplicate values that are of the name="prop".


    <script>

    function setPropField(formField,indx,secName,header){

    var frm = document.setPropertyForm;
    frm.prop.value = formField.value;
    frm.indx.value = indx;
    frm.secName.value = secName;
    frm.header.value = header;


    thisProp = formField.value;
    var dupeFrm = document.checkForPropDuplicatesForm;
    var propVals = dupeFrm.document.getElementsByName("prop");




    for( var i = 0; i < propVals.length; i++) {
    thisElem = propVals.value;

    if( thisProp == thisElem ) {
    alert("This is a duplictae value" +
    "\n Please create another" );
    return false;
    }

    }

    }

    </script>


    <% SortedMap props = (SortedMap) headers.get(sectionName);
    Set set = props.keySet();
    Iterator it = set.iterator();
    Object pval = "";
    Object vval = "";

    while (it.hasNext()) {
    pval = (Object) it.next();
    vval = (Object) props.get(pval);
    iii++;
    %>


    <tr>
    <td colspan="4" width="100%">
    <table class="summaryTable" width="100%">

    <tr class="tableTitle" style="border:0px;padding:0px;">
    <td colspan="4"><span class="style2">Add Property and
    Value</span></td>
    </tr>
    <tr class="headerRow firstColor">
    <td>ENV</td>
    <td>Name</td>
    <td>Value</td>
    </tr>
    <tr class="firstColor">
    <td><strong>ADA</strong></td>
    <td><input name="prop" id="prop" value="" size="50"
    onchange="setPropField(this,'<%=iii %>','<%=sectionName %>','<%=header
    %>');"></input></td>
    <td><input name="propVal" id="propVal" value="" size="85"
    onchange="setValField(this,'<%=iii %>','<%=sectionName %>','<%=header
    %>');"></input></td>
    </tr>
    <tr>
    <td colspan="3" align="right"><img onclick="submit();"
    src="images/btnBigSave.gif"></td>
    </table>
    </td>
    </tr>

    <% } %>
     
    da_rod_father, Mar 16, 2006
    #3
  4. da_rod_father

    Hal Rosser Guest

    Why not try giving each text box its own unique name - then accessing them
    that way ?
     
    Hal Rosser, Mar 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G
    Replies:
    5
    Views:
    553
    Craig G
    Feb 22, 2005
  2. Craig G

    Looping thru controls - part 2!!

    Craig G, Feb 22, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    367
    Craig G
    Feb 24, 2005
  3. Replies:
    16
    Views:
    828
    James Kanze
    Apr 12, 2007
  4. THTB
    Replies:
    0
    Views:
    197
  5. Max Williams
    Replies:
    3
    Views:
    176
    Robert Klemme
    Jan 6, 2009
Loading...

Share This Page