By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,628 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,628 IT Pros & Developers. It's quick & easy.

Javascript for value attribute

P: 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

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

brian.ackermann wrote:
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


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

Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
br*************@gmail.com enlightened us with...
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>>


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

Jul 23 '05 #4

P: n/a
kaeli wrote:
[snip]

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.


In Netscape 4 you could use:

value= &{variable};

Aah, TWTD.
Mick

Jul 23 '05 #5

P: n/a
brian.ackermann wrote:
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>


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
Jul 23 '05 #6

P: n/a
brian.ackermann wrote:
I decided to build the whole input line in javascript:
Reconsider.
<SCRIPT TYPE="text/javascript">
document.write("<INPUT TYPE='TEXT' NAME='AMOUNT'");
document.write(" VALUE='" + roundNumber(43.987654, 5) +"'");
document.write(">");
</SCRIPT>


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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.