[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Passing variables to a javascript function part 2.

Posted on 2004-10-29
12
Medium Priority
?
268 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
Comment
Question by:TrueBlue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
12 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12449913
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
ID: 12449952
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
ID: 12449985
this is really ugly but for the code you presesnted it is

 compPhone(this,this.parentNode.parentNode.childNodes[0].childNodes[2])
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12450030
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
ID: 12450053
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
devic earned 600 total points
ID: 12450187
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:
GwynforWeb earned 1400 total points
ID: 12450285
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
ID: 12450288
t
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12450290
sorry mispost :(
0
 

Author Comment

by:TrueBlue
ID: 12450443
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
ID: 12450526
change

   if ( saltphone==spriphone )

to


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

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

656 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