Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing two variables to a javascript function

Posted on 2004-10-29
18
Medium Priority
?
451 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
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
  • 6
  • 4
  • 4
  • +2
18 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12449160
it is this

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

Expert Comment

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

Expert Comment

by:StormyWaters
ID: 12449167
sorry, stale window.
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 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 400 total points
ID: 12449195
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
ID: 12449205
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
ID: 12449221
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
ID: 12449231
getElementById will pick up the field if it does not have an id field of another value
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12449237
sorry Cd& unrefreshed page
0
 

Author Comment

by:TrueBlue
ID: 12449264
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
ID: 12449285
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
devic earned 400 total points
ID: 12449373
    <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
ID: 12449467
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
ID: 12449485
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
StormyWaters earned 400 total points
ID: 12449498
Try putting quotes around this:
<INPUT id=priphone
ie,
<INPUT id="priphone"
0
 

Author Comment

by:TrueBlue
ID: 12449569
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:
GwynforWeb earned 800 total points
ID: 12449571
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
ID: 12449615
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
ID: 12449874
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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

618 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