Solved

Passing variables to a javascript function part 2.

Posted on 2004-10-29
266 Views
Last Modified: 2006-11-17
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>
0
Question by:TrueBlue
    12 Comments
     
    LVL 31

    Expert Comment

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

    Expert Comment

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

    Expert Comment

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

     compPhone(this,this.parentNode.parentNode.childNodes[0].childNodes[2])
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    taking the node route is dangerous as if you edit the page later the root will change, I advise diferrent id's
    0
     

    Author Comment

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

    Assisted Solution

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

    Accepted Solution

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

    Expert Comment

    by:GwynforWeb
    t
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    sorry mispost :(
    0
     

    Author Comment

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

    Expert Comment

    by:GwynforWeb
    change

       if ( saltphone==spriphone )

    to


       if ( saltphone==spriphone && !/^ *$/.test(saltphone))
    0
     
    LVL 31

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
    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…

    910 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

    14 Experts available now in Live!

    Get 1:1 Help Now