Passing two variables to a javascript function

Hi!

I am trying to pass two variables to a javascript function named compPhone.
The "this" part works fine, but the priphone variable is not being passed correctly.

Here is the problem line: compPhone(this,getElementsByName("priphone"));"

      <td align="center">
        <xsl:choose>
          <xsl:when test="$aspemail != 'true'">
            <!-- We're only checking for proper format here. -->
            <INPUT maxLength="12" name="priphone" 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(this,getElementsByName("priphone"));"
             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>

The javascript works properly if I do this, but obviously I need to be able to pass both variables.

function compPhone(altphone,priphone)
{
     spriphone ="212-555-1234"                     <<<<<<<<<< test code
     spriphone = priphone.value;
     saltphone = altphone.value;
     
     window.alert( spriphone  + ", " + saltphone );    
     
     if(saltphone==spriphone)
     {
          window.alert("Duplicate Telephone Numbers Not Permissible");
          altphone.value="";
          doSubmit = false;
          return;
     }
     doSubmit = true;
}

   
TrueBlueAsked:
Who is Participating?
 
GwynforWebConnect With a Mentor Commented:
this line should be  

   onblur="checkPhone(this); compPhone(this,document.getElementById('priphone'));"

eg

<input id="priphone" 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,document.getElementById('priphone'));"
onkeyup="phoneMask(this);" onkeydown="phoneMask(this);" value="{cal_aph}" />

<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>
0
 
GwynforWebCommented:
it is this

 compPhone(this,document.getElementById("priphone"))
0
 
StormyWatersCommented:
document.getElementById('priphone');
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
StormyWatersCommented:
sorry, stale window.
0
 
COBOLdinosaurConnect With a Mentor Commented:
getElementsByName

returns an array.  I think even with a single instance you will get an array with a length of one.

I would give the element an id and use getElementById instead,

or just pass the name, and use the getElementsByName method in the function.

Cd&

0
 
COBOLdinosaurCommented:
Well yes they should use an id but the code does not have an id on the input.

Cd&
0
 
GwynforWebCommented:
If you use document.getElementsByName remember it is an array of all objects with the given name, so for your usage you should be using document.getElementsByName("priphone")[0] , I recommend document.getElementById("priphone") though and add an id attribut id=priphone for earlier browsers, I am not sure of its browser compatiblity and it is not used a lot here.
 
0
 
GwynforWebCommented:
getElementById will pick up the field if it does not have an id field of another value
0
 
GwynforWebCommented:
sorry Cd& unrefreshed page
0
 
TrueBlueAuthor Commented:
Hi!

That is what I have now:

I added the id as suggested.

 <td align="center">
        <xsl:choose>
          <xsl:when test="$aspemail != 'true'">
            <!-- We're only checking for proper format here. -->
            <INPUT maxLength="12" name="priphone" id="priphone" 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(this,getElementById("priphone"));"
             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>

I get the following error:

Required white space was missing. Error processing resource 'https://www.secured.sample.com/calllist.xsl'. Line 21...

             onblur="checkPhone(this); compPhone(this,getElementById("priphone"));"
--------------------------------------...

If I change the double quotes to single quotes the error goes away, but I do not get the alert window.

Any ideas?
0
 
StormyWatersCommented:
SInce your using the same type of quote, it sees this:

onblur="checkPhone(this); compPhone(this,getElementById("

And then, for no reason, this:
priphone"));"

So change either the inner quotes(around 'priphone') or the outer quotes (around the whole thing) to '. I'm not sure why  it wouldn't display the alert with single quotes, if someone else should clarify?
0
 
devicConnect With a Mentor Commented:
    <td align="center">
       <xsl:choose>
         <xsl:when test="$aspemail != 'true'">
           <!-- We're only checking for proper format here. -->
           <INPUT id=priphone maxLength="12" name="priphone" 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(this,getElementById('priphone'));"
             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>

The javascript works properly if I do this, but obviously I need to be able to pass both variables.
<script>
function compPhone(altphone,priphone)
{
    spriphone ="212-555-1234"                     //<<<<<<<<<< test code
    spriphone = priphone.value;
    saltphone = altphone.value;
   
     window.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>
==============

this as is, shows alert, if the field lost focus.

note, I added for debug, two blank functions, to avoid error
If you don't have these functions, then you get error, before the alert.
0
 
TrueBlueAuthor Commented:
Hi!

So the alert window is working, but it only shows the value that is passed from "this".

Could it be that the single quotes around 'priphone' is syntacically correct, but not logically correct?

compPhone(this,getElementById('priphone'));"

I have the other two functions.

I just did not show them for simplicity.
0
 
devicCommented:
I wrote  in the first input aaa and in the second bbb

and I see alert below:

---------------------------
Microsoft Internet Explorer
---------------------------
aaa, bbb
---------------------------
OK  
---------------------------
0
 
StormyWatersConnect With a Mentor Commented:
Try putting quotes around this:
<INPUT id=priphone
ie,
<INPUT id="priphone"
0
 
TrueBlueAuthor Commented:
Hi!

So I have made a few changes with help from everyone here.
I set the id="primary".
I think it did not like having priphone for the name and id.

Now the alert window shows two values, however, for some reason it shows the primary as the value in the first row and the "this" changes as you changes rows. So I need to figure out how to show the primary value for the row that I currently on.

Any ideas?

Hope this makes sense.

function compPhone(priphone,altphone)
{
     spriphone = priphone.value;
     saltphone = altphone.value;
     
     window.alert( spriphone  + ", " + saltphone );    
     
     if(saltphone==spriphone)
     {
          window.alert("Duplicate Telephone Numbers Not Permissible");
          altphone.value="";
          doSubmit = false;
          return;
     }
     doSubmit = true;
}


   <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(getElementById('primary'),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>

0
 
TrueBlueAuthor Commented:
Hi GwynforWeb!

Thank you for the corrected syntax, but the primary field's value for all rows is the value of the first row.
It is not capturing the value for the second row if that is where you are on the grid. The this function seems to have no trouble with different rows.

Any ideas?
0
 
GwynforWebCommented:
I am not sure I am following. Do you have multiple rows like the one above, ie many elements with id='priphone' in which case the code will always refer to the first one.
0
All Courses

From novice to tech pro — start learning today.