Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in javascript coding

Status
Not open for further replies.

newme01

Programmer
Jun 19, 2012
7
PH
Hi guys,

I need your help with my simple problem

I want to add the value of textbox1 and textbox2 and then show the result using plain text

Here is my code:
----------------------
<HTML>
<HEAD>
<TITLE></TITLE>

</HEAD>
<BODY>

<script language="javascript">
function Addnow(form)
{
var TextBox1 = form.textname1.value;
var TextBox2 = form.textname2.value;
var txt = eval(TextBox1) + eval(TextBox2);
}
</script>

<form>
<INPUT type = text name="textname1" onChange="Addnow(this.form);">
<INPUT type = text name="textname2" onChange="Addnow(this.form);">
// how to display the value of "txt" in plain text only

</form>

</BODY>
</HTML>


 
Hi

I suppose by "show the result using plain text" you mean to display it in the document without using a [tt]form[/tt] element. Just use any element and set its [tt]innerHTML[/tt] :
HTML:
[b]<html>[/b]
[b]<head>[/b]
[b]<title></title>[/b]
[b]</head>[/b]
[b]<body>[/b]

[b]<script[/b] [maroon]language[/maroon][teal]=[/teal][green][i]"javascript"[/i][/green][b]>[/b]
[b]function[/b] [COLOR=darkgoldenrod]Addnow[/color][teal]([/teal]form[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] TextBox1 [teal]=[/teal] form[teal].[/teal]textname1[teal].[/teal]value[teal];[/teal]
  [b]var[/b] TextBox2 [teal]=[/teal] form[teal].[/teal]textname2[teal].[/teal]value[teal];[/teal]
  [b]var[/b] txt [teal]=[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]TextBox1[teal])[/teal] [teal]+[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]TextBox2[teal]);[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'result'[/i][/green][teal]).[/teal]innerHTML [teal]=[/teal] txt[teal];[/teal]
[teal]}[/teal]
[b]</script>[/b]

[b]<form>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]text name[/i][/green]=[green][i]"textname1"[/i][/green] [maroon]onchange[/maroon][teal]=[/teal][green][i]"Addnow(this.form);"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]text name[/i][/green]=[green][i]"textname2"[/i][/green] [maroon]onchange[/maroon][teal]=[/teal][green][i]"Addnow(this.form);"[/i][/green][b]>[/b]
[b]</form>[/b]

[b]<div[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"result"[/i][/green][b]></div>[/b]

[b]</body>[/b]
[b]</html>[/b]
Note that using [tt]eval()[/tt] is a bad habit.

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
[link feherke.github.com/][/url]
 
This is what I really want. thanks a lot.

feherke, if you dont mind i would like to ask why is it a bad habit to use eval()?


 
Hi

newme01 said:
i would like to ask why is it a bad habit to use eval()?
Sorry for not formulating a proper answer myself, but there are many explanations on the web, mostly written by people with better English skills than mine.

As a starting point I suggest the Mozilla Developer Network's [tt]eval()[/tt] reference which includes a Don't use eval! section.

For more results search the web for "eval is evil", that is the common buzzword of this subject.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top