Jump to content

Need help with a JavaScript that I made.


clonetrooper9494
 Share

Recommended Posts

I was making a JavaScript and it doesn't do what I want it to do. I want it to make the value of a text box the same as 2 other text boxes combined. But I am not sure how to... I am skimming through the sites tutorial, but that doesn't seem to help, its very confusing to me. If you know how to fix the problem, please tell me.The java script:

<script LANGUAGE="JavaScript">function fusethem() {var 1 = form.1.valuevar 2 = form.2.valueif (1 = "test")form.3.value = ("1" + "2")</script>

The Body:

<center><form name=idform>1:  <input type=text name=1 value=test><br>2:  <input type=text name=2 value=ing><br>3:  <input type=text name=3 onFocus="this.blur()"><br><input type="button" value="combine" onClick="java script:fusethem()" ></form></center>

Link to comment
Share on other sites

I was making a JavaScript and it doesn't do what I want it to do. I want it to make the value of a text box the same as 2 other text boxes combined. But I am not sure how to... I am skimming through the sites tutorial, but that doesn't seem to help, its very confusing to me. If you know how to fix the problem, please tell me.
A few changes needed to make it work:
  • Don't use a number as a javascript variable name (because a number already has a meaning as a number!)
  • Don't put javascript variable names in quotes
  • For cross-browser compatibility, use html element ids not names where possible
  • Use "document.getElementById" to access html elements
  • Don't include "java script:" in onclick event attributes (but when it is needed, in places like href attributes, it is one word "javascript", not two words)

The updated lines of code:

function fusethem() {var v1 = document.getElementById('1').value;var v2 = document.getElementById('2').value;document.getElementById('3').value = v1 + v2;}...1:  <input type=text id="1" value=test><br>2:  <input type=text id="2" value=ing><br>3:  <input type=text id="3" onFocus="this.blur()"><br><input type="button" value="combine" onClick="fusethem()" >

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...