Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

javascript: looping thru elements to find duplicates

Thread view: 
da_rod_father - 16 Mar 2006 18:55 GMT
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?
Hal Rosser - 16 Mar 2006 23:21 GMT
> Hi all,
>
[quoted text clipped - 7 lines]
>
> 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 .

HTH
da_rod_father - 17 Mar 2006 00:08 GMT
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[i].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>   

<% } %>
Hal Rosser - 18 Mar 2006 07:14 GMT
Why not try giving each text box its own unique name - then accessing them
that way ?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.