javascript: looping thru elements to find duplicates

D

da_rod_father

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?
 
H

Hal Rosser

da_rod_father said:
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 .

HTH
 
D

da_rod_father

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>

<% } %>
 
H

Hal Rosser

Why not try giving each text box its own unique name - then accessing them
that way ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top