Connecting Tech Pros Worldwide Help | Site Map

Javascript for value attribute

  #1  
Old July 23rd, 2005, 08:05 PM
brian.ackermann
Guest
 
Posts: n/a
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(number, digits); </script>>

Thanks,

Brian

  #2  
Old July 23rd, 2005, 08:05 PM
RobB
Guest
 
Posts: n/a

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(number, 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:

http://d0om.fnal.gov/d0admin/doctaur...pt/ch12_02.htm

  #3  
Old July 23rd, 2005, 08:05 PM
brian.ackermann
Guest
 
Posts: n/a

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

  #4  
Old July 23rd, 2005, 08:06 PM
kaeli
Guest
 
Posts: n/a

re: Javascript for value attribute


In article <1112991546.905588.308620@z14g2000cwz.googlegroups .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(number, digits); </script>>
>[/color]

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

value="<%=expression%>"

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.formname.amount.value = roundNumber(number, 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.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

  #5  
Old July 23rd, 2005, 08:06 PM
Mick White
Guest
 
Posts: n/a

re: Javascript for value attribute


kaeli wrote:
[snip][color=blue]
>
> You can't do that...unless you're doing server-side stuff with
>
> value="<%=expression%>"
>
> 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

  #6  
Old July 23rd, 2005, 08:06 PM
RobG
Guest
 
Posts: n/a

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
  #7  
Old July 23rd, 2005, 08:10 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy table cell text into an input as value attribute charles-brewster@ntlworld.com answers 2 July 13th, 2006 08:20 PM
changing css bu javascript for all input boxes with the same name amykimber@gmail.com answers 13 May 19th, 2006 04:05 AM
how can I used javascript for chaging values in TextBox Anton ml. Vahčič answers 2 November 18th, 2005 04:24 AM
javascript for putting text in an asp.net edit control Michelle Stone answers 1 November 17th, 2005 09:57 PM