Connecting Tech Pros Worldwide Help | Site Map

Javascript for value attribute

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

Thanks,

Brian


  #2  
Old July 23rd, 2005, 07:05 PM
RobB
Guest
 
Posts: n/a
Default 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, 07:05 PM
brian.ackermann
Guest
 
Posts: n/a
Default 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, 07:06 PM
kaeli
Guest
 
Posts: n/a
Default 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, 07:06 PM
Mick White
Guest
 
Posts: n/a
Default 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, 07:06 PM
RobG
Guest
 
Posts: n/a
Default 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, 07:10 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.