Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to compare 2 elements with same getElementById(x)?

Reply
Thread Tools

How to compare 2 elements with same getElementById(x)?

 
 
vadymus@gmail.com
Guest
Posts: n/a
 
      12-22-2005
Hello,
I need to compare 2 id names but not their values. For example:

document.getElementById(i) == document.getElementById(j)

these elements above seem to give error or do not work.
My original statement is this (and note, when I add line above to it,
that does not work):

function uniqueValues(){
var myForm = document.add2db;
var iMax = myForm.imax.value;
//var IsSimilar, IsSame;
for (i=0;i<iMax;i++)
{

var fieldValue=document.getElementById(i).value;
alert("id is "+document.getElementById(i));
for(j=0;j<iMax;j++)
{
var fieldValue2=document.getElementById(j).value;
if ((fieldValue == fieldValue2) && (document.getElementById(i) !=
document.getElementById(j))) { //FIX HERE
alert("I am sorry, but these values are identical:
"+document.getElementById(j).value);
document.getElementById(j).focus();
return false;
}
}
}
//return true;
}

 
Reply With Quote
 
 
 
 
bwucke@gmail.com
Guest
Posts: n/a
 
      12-22-2005
> I need to compare 2 id names but not their values. For example:
> document.getElementById(i) == document.getElementById(j)


Well, there's this method, .isEqualNode() but since IDs are supposed to
be unique, in your case i==j should perfectly suffice.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      12-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) said:
>
>Hello,
>I need to compare 2 id names but not their values. For example:
>
>document.getElementById(i) == document.getElementById(j)
>
>these elements above seem to give error or do not work.


Maybe I'm missing something, but why don't you just compare
the two id's? "if (i==j)". You should never have two elements
with the same id.

 
Reply With Quote
 
vadymus@gmail.com
Guest
Posts: n/a
 
      12-22-2005
Thank you so much...
I did finf .name but it did not work for me. Identical id's are not
recognized (maybe I do smth wrong?):

function uniqueValues(){
var myForm = document.add2db;
var iMax = myForm.imax.value;
for (i=0;i<iMax;i++)
{

var fieldValue=document.getElementById(i).value;
//alert("id is "+document.getElementById(i).name);
for(j=0;j<iMax;j++)
{
var fieldValue2=document.getElementById(j).value;
if ((fieldValue == fieldValue2) && (document.getElementById(i).name
!= document.getElementById(j).name)) { //FIX HERE
alert("I am sorry, but these values are identical:
"+document.getElementById(j).value);
document.getElementById(j).focus();
return false;
}
}
}
//return true;
}



I am going to try .isEqualNode() now

 
Reply With Quote
 
vadymus@gmail.com
Guest
Posts: n/a
 
      12-22-2005
Lee, you are absolutely right i and j can be compared. I cannot believe
it is more simple that I thought. However, my script does not catch
identical values here still.

I try to submit form with unique field values as below.
- imax is the number of fields.
- all unique field values are from textarea called texarea0, textarea1,
.......texareaN (all together is imax)
- these texarea fields have unique html IDs: 0, 1, 2, 3.....N

Code below does not catch nonubique values eventhough there are no
errors when run

function uniqueValues(){
var myForm = document.add2db;
var iMax = myForm.imax.value;
for (i=0;i<iMax;i++)
{

var fieldValue=document.getElementById(i).value;
//alert("id is "+document.getElementById(i).name);
for(j=0;j<iMax;j++)
{
var fieldValue2=document.getElementById(j).value;
if ((fieldValue == fieldValue2) && (i!=j)) { //FIX
HEREdocument.getElementById(i).name != document.getElementById(j).name
alert("I am sorry, but these values are identical:
"+document.getElementById(j).value);
document.getElementById(j).focus();
return false;
}
}
}
//return true;
}

 
Reply With Quote
 
bwucke@gmail.com
Guest
Posts: n/a
 
      12-23-2005

(E-Mail Removed) napisal(a):
> Lee, you are absolutely right i and j can be compared. I cannot believe
> it is more simple that I thought. However, my script does not catch
> identical values here still.
>
> I try to submit form with unique field values as below.
> - imax is the number of fields.
> - all unique field values are from textarea called texarea0, textarea1,
> .......texareaN (all together is imax)
> - these texarea fields have unique html IDs: 0, 1, 2, 3.....N
>
> Code below does not catch nonubique values eventhough there are no
> errors when run
>
> function uniqueValues(){
> var myForm = document.add2db;
> var iMax = myForm.imax.value;
> for (i=0;i<iMax;i++)
> {
>
> var fieldValue=document.getElementById(i).value;
> //alert("id is "+document.getElementById(i).name);
> for(j=0;j<iMax;j++)
> {
> var fieldValue2=document.getElementById(j).value;
> if ((fieldValue == fieldValue2) && (i!=j)) { //FIX
> HEREdocument.getElementById(i).name != document.getElementById(j).name
> alert("I am sorry, but these values are identical:
> "+document.getElementById(j).value);
> document.getElementById(j).focus();
> return false;
> }
> }
> }
> //return true;
> }


Not sure what's the problem now, but I see you're doing quite a few too
many checks. That is, say, i=3, j=5, then j=3, i=5 will check the same.

for (i=0; i < iMax-1 ; i++)...
{
....
for(j=i+1;j<iMax;j++)
{
....
if (fieldValue == fieldValue2)
{

then i will never be equal to j and still all pairs get checked.

Maybe send a piece of HTML, possible that you're mishandling IDs there
somehow.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      12-23-2005
(E-Mail Removed) wrote:
> Lee, you are absolutely right i and j can be compared. I cannot believe
> it is more simple that I thought. However, my script does not catch
> identical values here still.
>
> I try to submit form with unique field values as below.
> - imax is the number of fields.
> - all unique field values are from textarea called texarea0, textarea1,
> .......texareaN (all together is imax)
> - these texarea fields have unique html IDs: 0, 1, 2, 3.....N


This indicates that your HTML is invalid. IDs can include numbers,
but can't start with them.


>
> Code below does not catch nonubique values eventhough there are no
> errors when run


The following loops through all the elements of the form and barfs
when the first duplicate value is reached:

function uniqueValues()
{
var val, vals = {};
var el, els = document.forms['add2db'].elements;

for (var i=0, len=els.length; i<len; ++i){
el = els[i];
val = el.value
if (vals[val]){
alert('Already have ' + val);
if (el.focus) el.focus();
return;
}
vals[val] = val;
}
}


[...]


--
Rob
 
Reply With Quote
 
bwucke@gmail.com
Guest
Posts: n/a
 
      12-23-2005
> val = el.value
> if (vals[val]){ }
> vals[val] = val;


except if the user enters two zeros.

vals[val] = true;

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-23-2005
(E-Mail Removed) wrote:

>> val = el.value
>> if (vals[val]){ }
>> vals[val] = val;

>
> except if the user enters two zeros.
>
> vals[val] = true;


What are you talking about? `vals' is a reference to an Object object.
Property names as well as form controls' values are _string_ values:

var vals = {"0": "0"}, val = "00";
if (vals[val])
{
// will never be executed
}

One problem that remains, however, is that Object objects inherit
properties from Object, so that test is not reliable; for retaining
and those inherited property values and avoiding false positives, it
is necessary to map form controls' values that correspond to those
properties' names to unused property names; one may instead also use
Object.prototype.hasOwnProperty() where supported.

Please quote the minimum of what you are replying to and provide
attribution of quoted material:

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>


PointedEars
 
Reply With Quote
 
vadymus@gmail.com
Guest
Posts: n/a
 
      12-23-2005
Hello,
The code offered by RobG returns "Already have 0" alert and exists. It
does not find all similar values. Please, suggest something, if
possible...

function uniqueValues()
{
var val, vals = {};
var el, els = document.forms['add2db'].elements;

for (var i=0, len=els.length; i<len; ++i){
el = els[i];
val = el.value
if (vals[val]){
alert('Already have ' + val);
if (el.focus) el.focus();
return;
}
vals[val] = val;
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
algorithm to compare 3 array elements css-discuss.org Javascript 11 09-28-2011 02:29 AM
STL algorithm to compare if 2 vector<int> have same value? silverburgh.meryl@gmail.com C++ 4 10-29-2008 09:35 AM
How compare the same entity in JPQL ? marek Java 1 03-12-2008 01:32 AM
Perl Newbie: compare two files contents with same pattern YC Perl 1 08-13-2003 10:42 PM



Advertisments