Passing variables to a javascript function part 2.

Hi!

I am trying to pass two variables to a javascript function.
I have a datagrid.

This statement below:
compPhone(document.getElementById('primary'),this);"

Yields the correct second parameter, but the first parameter is always the value of the first field in the first row.

So I tried this:

compPhone(document.getElementById('primary'[position()]),this);"

but I get no values.

Is the syntax wrong or invalid?

Thank you in advance.

 <td align="center">
        <xsl:choose>
          <xsl:when test="$aspemail != 'true'">
            <!-- We're only checking for proper format here. -->
            <INPUT maxLength="12" name="priphone" id="primary" size="12" onfocus="doSubmit=false;"
             onblur="checkPhone(this);" onkeyup="phoneMask(this);" onkeydown="phoneMask(this);"
             value="{cal_mph}"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="cal_mph"/>&#160;
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <td align="center">
        <xsl:choose>
          <xsl:when test="$aspemail != 'true'">
            <!-- We're only checking for proper format here. -->
            <INPUT maxLength="12" name="altphone" size="12" onfocus="doSubmit=false;"
             onblur="checkPhone(this); compPhone(document.getElementById('primary'[position()]),this);"
             onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="cal_aph"/>&#160;
          </xsl:otherwise>
        </xsl:choose>
      </td>
TrueBlueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
this ?


<body>
<table border="1" width="398" height="148">
  <tr>
    <td width="398" height="64">
    <input id="priphone" maxLength="12" name="priphone" size="12"
    onfocus="doSubmit=false;" onblur="checkPhone(this);" onkeyup="phoneMask(this);"
    onkeydown="phoneMask(this);" value="{cal_mphA}" />
    <input maxLength="12" name="altphone"
    size="12" onfocus="doSubmit=false;"
    onblur="checkPhone(this); compPhone(this,this.parentNode.childNodes[0]);"
    onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}" /> </td>
  </tr>
  <tr>
    <td width="398" height="72">
    <input id="priphone" maxLength="12" name="priphone" size="12"
    onfocus="doSubmit=false;" onblur="checkPhone(this);" onkeyup="phoneMask(this);"
    onkeydown="phoneMask(this);" value="{cal_mphB}" />
    <input maxLength="12" name="altphone"
    size="12" onfocus="doSubmit=false;"
    onblur="checkPhone(this); compPhone(this,this.parentNode.childNodes[0]);"
    onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}" /> </td>
  </tr>
</table>

<script>
function compPhone(altphone,priphone)
{
    spriphone ="212-555-1234"                     //<<<<<<<<<< test code
    spriphone = priphone.value;
    saltphone = altphone.value;
   
    alert( spriphone  + ", " + saltphone );    
   
     if(saltphone==spriphone)
    {
         window.alert("Duplicate Telephone Numbers Not Permissible");
         altphone.value="";
         doSubmit = false;
         return;
    }
    doSubmit = true;
}
function checkPhone(){}
function phoneMask(){}
</script>
</body>
0
GwynforWebCommented:
the above code works but when you all the other stuff the node number will change I am not sure what it will be with <xsl:> tags. The other alternative is to give the input fileds differnt id's  eg primary1, primary2, .... and then getElementById will work
0
GwynforWebCommented:
this is really ugly but for the code you presesnted it is

 compPhone(this,this.parentNode.parentNode.childNodes[0].childNodes[2])
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GwynforWebCommented:
taking the node route is dangerous as if you edit the page later the root will change, I advise diferrent id's
0
TrueBlueAuthor Commented:
Hi!

Why can't I retrieve the primary as an array and just select the current row?

How would I have a dynamic id?

Something like <input id='primary' + position() ...

Thank you in advance.
0
devicCommented:
hi TrueBlue,

here another way, where you don't need anymore any id
=====================================
<form>
<input maxLength="12" name="priphone" size="12" onfocus="doSubmit=false;"
onblur="checkPhone(this);" onkeyup="phoneMask(this);" onkeydown="phoneMask(this);"
value="{cal_mph}" />

<input maxLength="12" name="altphone" size="12"
onfocus="doSubmit=false;"
onblur="checkPhone(this); compPhone(this);"
onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}" />

<hr>

<input maxLength="12" name="priphone" size="12" onfocus="doSubmit=false;"
onblur="checkPhone(this);" onkeyup="phoneMask(this);" onkeydown="phoneMask(this);"
value="aaa" />

<input maxLength="12" name="altphone" size="12"
onfocus="doSubmit=false;"
onblur="checkPhone(this); compPhone(this);"
onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="bbb" />

</form>
<script>
function compPhone(altphone)
{
   priphone = getPrevField(altphone)                     //<<<<<<<<<< test code
   spriphone = priphone.value;
   saltphone = altphone.value;
 
  alert( spriphone  + ", " + saltphone );    
 
     if(saltphone==spriphone)
   {
        window.alert("Duplicate Telephone Numbers Not Permissible");
        altphone.value="";
        doSubmit = false;
        return;
   }
   doSubmit = true;
}
function getPrevField(obj)
{
      var el=obj.form.elements;
      for(var i=0;i<el.length;i++)
      if(el[i]==obj)
      return el[i-1];
}



//////////
function checkPhone(){}
function phoneMask(){}
</script>
0
GwynforWebCommented:
this does it

function compPhone(altphone)
{
   alt=document.getElementsByName('altphone')
   for (i=0;i<alt.length;i++)
   if (alt[i]==altphone){
       spriphone=document.getElementsByName('priphone')[i].value
    }

    saltphone = altphone.value;
    alert( spriphone  + ", " + saltphone);  
     
    if(saltphone==spriphone)
    {
         window.alert("Duplicate Telephone Numbers Not Permissible");
         altphone.value="";
         doSubmit = false;
         return;
    }
    doSubmit = true;
}
function checkPhone(){}
function phoneMask(){}
</script>

<table>
  <tr>
    <td align="center"><xsl:choose> <xsl:when test="$aspemail != 'true'"> <!-- We're only checking for proper format here. -->
      <input maxLength="12" name="priphone" id="primary" size="12"
      onfocus="doSubmit=false;" onblur="checkPhone(this);" onkeyup="phoneMask(this);"
      onkeydown="phoneMask(this);" value="{cal_mph}A" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="cal_mph" />&nbsp; </xsl:otherwise> </xsl:choose>
   </td>
    <td align="center"><xsl:choose> <xsl:when test="$aspemail != 'true'"> <!-- We're only checking for proper format here. -->
      <input maxLength="12" name="altphone" size="12"
      onfocus="doSubmit=false;" onblur="checkPhone(this); compPhone(this);"
      onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="cal_aph" />&nbsp; </xsl:otherwise> </xsl:choose>
  </td>
  </tr>
  <tr>
    <td align="center"><xsl:choose> <xsl:when test="$aspemail != 'true'"> <!-- We're only checking for proper format here. --> <input maxLength="12" name="priphone" id="primary" size="12"
      onfocus="doSubmit=false;" onblur="checkPhone(this);" onkeyup="phoneMask(this);"
      onkeydown="phoneMask(this);" value="{cal_mph}B" /> </xsl:when> <xsl:otherwise>
   <xsl:value-of select="cal_mph" />&nbsp; </xsl:otherwise> </xsl:choose>
  </td>
    <td align="center"><xsl:choose> <xsl:when test="$aspemail != 'true'"> <!-- We're only checking for proper format here. --> <input maxLength="12" name="altphone" size="12"
      onfocus="doSubmit=false;" onblur="checkPhone(this); compPhone(this);"
      onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}" /> </xsl:when> <xsl:otherwise>
   <xsl:value-of select="cal_aph" />&nbsp; </xsl:otherwise> </xsl:choose>
  </td>
  </tr>
</table>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GwynforWebCommented:
t
0
GwynforWebCommented:
sorry mispost :(
0
TrueBlueAuthor Commented:
Hi GwynforWeb!

The only other thing I need is a way so that if both the pri and alt are empty the alert window is not  displayed.

Thank you in advance.

0
GwynforWebCommented:
change

   if ( saltphone==spriphone )

to


   if ( saltphone==spriphone && !/^ *$/.test(saltphone))
0
GwynforWebCommented:
you might want to try this as well  it will not alert if both are the same or different numbers of blank spaces

  if  ( saltphone==spriphone  && !(/^ *$/.test(spriphone) && /^ *$/.test(saltphone))  )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.