Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript for value attribute

brian.ackermann
Guest
 
Posts: n/a
#1: Jul 23 '05
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


RobB
Guest
 
Posts: n/a
#2: Jul 23 '05

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

brian.ackermann
Guest
 
Posts: n/a
#3: Jul 23 '05

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

kaeli
Guest
 
Posts: n/a
#4: Jul 23 '05

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

Mick White
Guest
 
Posts: n/a
#5: Jul 23 '05

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

RobG
Guest
 
Posts: n/a
#6: Jul 23 '05

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
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#7: Jul 23 '05

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