Javascript for value attribute

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

    #1

    Javascript for value attribute

    Hi,

    Can someone tell me what I'm doing wrong here? It makes sense to me,
    but fails to actually execute the code. I can't find anything on
    google, but maybe my search criteria are off.

    <input type="text" name="amount" value=<script TYPE="text/javascript">
    roundNumber(num ber, digits); </script>>

    Thanks,

    Brian

  • RobB

    #2
    Re: Javascript for value attribute


    brian.ackermann wrote:[color=blue]
    > Hi,
    >
    > Can someone tell me what I'm doing wrong here? It makes sense to me,
    > but fails to actually execute the code. I can't find anything on
    > google, but maybe my search criteria are off.
    >
    > <input type="text" name="amount" value=<script[/color]
    TYPE="text/javascript">[color=blue]
    > roundNumber(num ber, digits); </script>>
    >
    > Thanks,
    >
    > Brian[/color]

    Hi Brian.

    That 'value' attribute takes a string - you can't simply "plug in"
    JavaScript anywhere and expect it to run. Read this:



    Comment

    • brian.ackermann

      #3
      Re: Javascript for value attribute

      Yeah, so I am discovering.

      I decided to build the whole input line in javascript:

      <SCRIPT TYPE="text/javascript">
      document.write( "<INPUT TYPE='TEXT' NAME='AMOUNT'") ;
      document.write( " VALUE='" + roundNumber(43. 987654, 5) +"'");
      document.write( ">");
      </SCRIPT>

      Thanks. :D

      Comment

      • kaeli

        #4
        Re: Javascript for value attribute

        In article <1112991546.905 588.308620@z14g 2000cwz.googleg roups.com>,
        brian.ackermann @gmail.com enlightened us with...[color=blue]
        > Hi,
        >
        > Can someone tell me what I'm doing wrong here? It makes sense to me,
        > but fails to actually execute the code. I can't find anything on
        > google, but maybe my search criteria are off.
        >
        > <input type="text" name="amount" value=<script TYPE="text/javascript">
        > roundNumber(num ber, digits); </script>>
        >[/color]

        You can't do that...unless you're doing server-side stuff with

        value="<%=expre ssion%>"

        or similar. Which may be where you got the notion this was even vaguely
        possible.

        Do this instead.
        <input type="text" name="amount" value="">
        <script TYPE="text/javascript">
        document.formna me.amount.value = roundNumber(num ber, digits);
        </script>

        Note that "number" and "digits" must be in scope, available at the time it
        runs.

        --
        --
        ~kaeli~
        Murphy's Law #2030: If at first you don't succeed, destroy
        all evidence that you tried.



        Comment

        • Mick White

          #5
          Re: Javascript for value attribute

          kaeli wrote:
          [snip][color=blue]
          >
          > You can't do that...unless you're doing server-side stuff with
          >
          > value="<%=expre ssion%>"
          >
          > or similar. Which may be where you got the notion this was even vaguely
          > possible.[/color]

          In Netscape 4 you could use:

          value= &{variable};

          Aah, TWTD.
          Mick

          Comment

          • RobG

            #6
            Re: Javascript for value attribute

            brian.ackermann wrote:[color=blue]
            > Yeah, so I am discovering.
            >
            > I decided to build the whole input line in javascript:
            >
            > <SCRIPT TYPE="text/javascript">
            > document.write( "<INPUT TYPE='TEXT' NAME='AMOUNT'") ;
            > document.write( " VALUE='" + roundNumber(43. 987654, 5) +"'");
            > document.write( ">");
            > </SCRIPT>[/color]

            That's a bit silly, you are creating a dependency on JavaScript that
            is simply not required. Why not just use HTML?

            <input type="text" name="amount" value="43.98765 ">

            Then you have zero dependency on JavaScript. Presumably any rounding
            required for the value can be done on the server when the value is
            put into the page in the first place.

            If you can't control what the server puts into the page, then run the
            rounding function on page load. That way, everyone can seen the
            value but those with JavaScript enabled will see the rounded value.

            If you want a user-entered value to be rounded, then use an onchange
            event.


            --
            Rob

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Javascript for value attribute

              brian.ackermann wrote:
              [color=blue]
              > I decided to build the whole input line in javascript:[/color]

              Reconsider.
              [color=blue]
              > <SCRIPT TYPE="text/javascript">
              > document.write( "<INPUT TYPE='TEXT' NAME='AMOUNT'") ;
              > document.write( " VALUE='" + roundNumber(43. 987654, 5) +"'");
              > document.write( ">");
              > </SCRIPT>[/color]

              Consecutive calls of document.write( ) are inefficient and error-prone.
              Try this instead:

              document.write(
              "<input name='AMOUNT'" value='" + roundNumber(43. 987654, 5) + "'>");

              or this:

              document.write(
              ["<input name='AMOUNT'" value='", roundNumber(43. 987654, 5), "'>"]
              .join(""));


              PointedEars

              Comment

              Working...