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?
 
GwynforWebConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
GwynforWebCommented:
this is really ugly but for the code you presesnted it is

 compPhone(this,this.parentNode.parentNode.childNodes[0].childNodes[2])
0
 
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
 
devicConnect With a Mentor Commented:
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:
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
All Courses

From novice to tech pro — start learning today.