passing variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed

    passing variables

    I need to know how to pass variables because I want to use the same
    function to varify multiple data imput boxes.

    Please tell me what's wrong with this code.

    <head>
    <script language="JavaS cript">
    function myFunction(this ){
    entry=document. Forms[0].this.value;
    if (document.Forms[0].this.value=="" ){
    document.Forms[0].this.focus();
    }
    else {
    // Do something with "entry" here
    }
    if (this = "inputBox2" ) {
    document.Forms[0].total.value = document.Forms[0].inputBox1.valu e +
    document.Forms[0].inputBox2.valu e;
    }
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="inputBox1 " onBlur="myFunct ion(this)">
    <input type="text" name="inputBox2 " onBlur="myFunct ion(this)">
    <input type="text" name="total">
    </form>
    </body>

  • Grant Wagner

    #2
    Re: passing variables

    Ed wrote:
    [color=blue]
    > I need to know how to pass variables because I want to use the same
    > function to varify multiple data imput boxes.
    >
    > Please tell me what's wrong with this code.
    >
    > <head>
    > <script language="JavaS cript">
    > function myFunction(this ){[/color]

    "this" is a keyword representing the current object, it shouldn't and can't
    be used as a parameter name.

    Try

    function myFunction(refe renceToAnInput) {
    [color=blue]
    > entry=document. Forms[0].this.value;[/color]

    You've got a reference to the form element itself, it's unnecessary to
    reference the input this way. Simply use:

    var entry = referenceToAnIn put.value;
    [color=blue]
    > if (document.Forms[0].this.value=="" ){[/color]

    You store the input's value in a variable then you don't use it. If you're
    retrieving "entry", then make use of it:

    if (entry == "") {
    [color=blue]
    > document.Forms[0].this.focus();[/color]

    referenceToAnIn put.focus();
    [color=blue]
    > }
    > else {
    > // Do something with "entry" here
    > }
    > if (this = "inputBox2" ) {[/color]

    if (referenceToAnI nput.name == "inputBox2" ) {
    [color=blue]
    > document.Forms[0].total.value = document.Forms[0].inputBox1.valu e
    > +document.Forms[0].inputBox2.valu e;[/color]

    var referenceToTheF orm = referenceToAnIn put.form;
    referenceToTheF orm.total.value =
    parseFloat(refe renceToTheForm. inputBox1.value ) + parseFloat(entr y);

    You need to parseFloat() because the values in input boxes are strings, if
    you simply added them together with "+", it would concatenate them, not add
    their numeric values (see <url: http://jibbering.com/faq/#FAQ4_21 />)

    Since you retrieve "entry" at the beginning of the function, you might as
    well use it here rather then retrieving it again.
    [color=blue]
    > }
    > }
    > </script>
    > </head>
    > <body>
    > <form>
    > <input type="text" name="inputBox1 " onBlur="myFunct ion(this)">
    > <input type="text" name="inputBox2 " onBlur="myFunct ion(this)">
    > <input type="text" name="total">
    > </form>
    > </body>[/color]

    Note that by using the onblur event, and putting focus back on the input
    when the input is invalid, you've trapped the user in that input until they
    enter something, and created a situation that could lead to an endless loop
    where they blur the input, you put the focus back on it, some other sequence
    of events blurs it again and so on.

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Gain technical skills through documentation and training, earn certifications and connect with the community


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Ely

      #3
      Re: passing variables

      I've played with your code for hours and keep getting "Expecting an Object"

      Grant Wagner wrote:[color=blue]
      >
      > Ed wrote:
      >[color=green]
      > > I need to know how to pass variables because I want to use the same
      > > function to varify multiple data imput boxes.
      > >
      > > Please tell me what's wrong with this code.
      > >
      > > <head>
      > > <script language="JavaS cript">
      > > function myFunction(this ){[/color]
      >
      > "this" is a keyword representing the current object, it shouldn't and can't
      > be used as a parameter name.
      >
      > Try
      >
      > function myFunction(refe renceToAnInput) {
      >[color=green]
      > > entry=document. Forms[0].this.value;[/color]
      >
      > You've got a reference to the form element itself, it's unnecessary to
      > reference the input this way. Simply use:
      >
      > var entry = referenceToAnIn put.value;
      >[color=green]
      > > if (document.Forms[0].this.value=="" ){[/color]
      >
      > You store the input's value in a variable then you don't use it. If you're
      > retrieving "entry", then make use of it:
      >
      > if (entry == "") {
      >[color=green]
      > > document.Forms[0].this.focus();[/color]
      >
      > referenceToAnIn put.focus();
      >[color=green]
      > > }
      > > else {
      > > // Do something with "entry" here
      > > }
      > > if (this = "inputBox2" ) {[/color]
      >
      > if (referenceToAnI nput.name == "inputBox2" ) {
      >[color=green]
      > > document.Forms[0].total.value = document.Forms[0].inputBox1.valu e
      > > +document.Forms[0].inputBox2.valu e;[/color]
      >
      > var referenceToTheF orm = referenceToAnIn put.form;
      > referenceToTheF orm.total.value =
      > parseFloat(refe renceToTheForm. inputBox1.value ) + parseFloat(entr y);
      >
      > You need to parseFloat() because the values in input boxes are strings, if
      > you simply added them together with "+", it would concatenate them, not add
      > their numeric values (see <url: http://jibbering.com/faq/#FAQ4_21 />)
      >
      > Since you retrieve "entry" at the beginning of the function, you might as
      > well use it here rather then retrieving it again.
      >[color=green]
      > > }
      > > }
      > > </script>
      > > </head>
      > > <body>
      > > <form>
      > > <input type="text" name="inputBox1 " onBlur="myFunct ion(this)">
      > > <input type="text" name="inputBox2 " onBlur="myFunct ion(this)">
      > > <input type="text" name="total">
      > > </form>
      > > </body>[/color]
      >
      > Note that by using the onblur event, and putting focus back on the input
      > when the input is invalid, you've trapped the user in that input until they
      > enter something, and created a situation that could lead to an endless loop
      > where they blur the input, you put the focus back on it, some other sequence
      > of events blurs it again and so on.
      >
      > --
      > | Grant Wagner <gwagner@agrico reunited.com>
      >
      > * Client-side Javascript and Netscape 4 DOM Reference available at:
      > *
      > http://devedge.netscape.com/library/...ce/frames.html
      >
      > * Internet Explorer DOM Reference available at:
      > *
      > http://msdn.microsoft.com/workshop/a...ence_entry.asp
      >
      > * Netscape 6/7 DOM Reference available at:
      > * http://www.mozilla.org/docs/dom/domref/
      > * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
      > * http://www.mozilla.org/docs/web-deve...upgrade_2.html[/color]

      Comment

      Working...