Solved

Passing two variables to a javascript function

Posted on 2004-10-29
444 Views
Last Modified: 2012-06-27
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;
}

   
0
Question by:TrueBlue
    18 Comments
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    it is this

     compPhone(this,document.getElementById("priphone"))
    0
     
    LVL 13

    Expert Comment

    by:StormyWaters
    document.getElementById('priphone');
    0
     
    LVL 13

    Expert Comment

    by:StormyWaters
    sorry, stale window.
    0
     
    LVL 53

    Assisted Solution

    by:COBOLdinosaur
    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
     
    LVL 53

    Expert Comment

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

    Cd&
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    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
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    getElementById will pick up the field if it does not have an id field of another value
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    sorry Cd& unrefreshed page
    0
     

    Author Comment

    by:TrueBlue
    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
     
    LVL 13

    Expert Comment

    by:StormyWaters
    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
     
    LVL 25

    Assisted Solution

    by:devic
        <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
     

    Author Comment

    by:TrueBlue
    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
     
    LVL 25

    Expert Comment

    by:devic
    I wrote  in the first input aaa and in the second bbb

    and I see alert below:

    ---------------------------
    Microsoft Internet Explorer
    ---------------------------
    aaa, bbb
    ---------------------------
    OK  
    ---------------------------
    0
     
    LVL 13

    Assisted Solution

    by:StormyWaters
    Try putting quotes around this:
    <INPUT id=priphone
    ie,
    <INPUT id="priphone"
    0
     

    Author Comment

    by:TrueBlue
    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
     
    LVL 31

    Accepted Solution

    by:
    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
     

    Author Comment

    by:TrueBlue
    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
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now