0

the following values are to calculate the user entered values, these are calculated and passed to the text field,

if(computer && monitor && tv && laptop && cell)
      { // getting the text field the values and calculated 
         var valueCom = document.getElementById("computer").value ;
         var valueMon = document.getElementById("monitor").value ; 
         var valueTv  = document.getElementById("tv").value ; 
         var valueLap = document.getElementById("laptop").value ;
         var valueCel = document.getElementById("cell").value;

         var finalCom = valueCom * 0.1818937134 ;        
         var finalMon = valueMon * 0.056842 ;    
         var finalTv  = valueTv  * 0.056842 ;           
         var finalLap = valueLap * 0.090947 ;        
         var finalCel = valueCel * 0.045473 ;     

         var totalTonnes = finalCom + finalMon + finalTv + finalLap + finalCel; 

         var totalCarbon  = totalTonnes * 1 ; 
         var totalTree    = totalTonnes * 17.1969 ;
         var totalPetrol  = totalTonnes * 286.396 ;
         var totalPlastic = totalTonnes * 646.421 ; 
// pass this above four values to the textfield

      }
<input type="text" name="carbon" >
<input type="text" name="tree" >
<input type="text" name="petrol" >
<input type="text" name="plastic" >
// field to pass values here

how to pass this values using java script to the text field. can anyone help me please

1
  • where you want to set ? Commented Mar 8, 2016 at 6:01

6 Answers 6

1

you want to add id to text field,

<input type="text" name="carbon" id="carbon">
<input type="text" name="tree" id="tree">
<input type="text" name="petrol" id="petrol">
<input type="text" name="plastic" id="plastic">

then after javascript,

document.getElementById("carbon").value=totalCarbon; 
document.getElementById("tree").value=totalTree;    
document.getElementById("petrol").value=totalPetrol;  
document.getElementById("plastic").value=totalPlastic; 

and also you can use to value set by name,

document.getElementsByName("plastic")[0].value = totalPlastic;
......

or,

 document.getElementById("plastic").setAttribute('value',totalCarbon);
.....
Sign up to request clarification or add additional context in comments.

Comments

0

Assign your resultant text field with id="result" or anything. Then, you can put your result as $(#result).val(yourCalcultedResult);

2 Comments

Except the OP hasn't tagged the question with a library nor indicated its use in the question. You haven't said which library you're using, and there's a syntax error.
this code using only javascript and html. not include jquery.
0

set the value property

document.getElementById("carbon").value = totalCarbon;
document.getElementById("tree").value = totalTree;
document.getElementById("petrol").value = totalPetrol;
document.getElementById("plastic").value = totalPlastic;

and set the ids to the respective elements

<input type="text" name="carbon" id="carbon" >
<input type="text" name="tree" id="tree" >
<input type="text" name="petrol" id="petrol" >
<input type="text" name="plastic" id="plastic" >

Or if you still want to use names only, then make it

document.getElementsByName("carbon")[0].value = totalCarbon;
document.getElementsByName("tree")[0].value = totalTree;
document.getElementsByName("petrol")[0].value = totalPetrol;
document.getElementsByName("plastic")[0].value = totalPlastic;

Comments

0
document.getElementsByName("carbon")[0].value = totalCarbon;
document.getElementsByName("tree")[0].value = totalTree;
document.getElementsByName("petrol")[0].value = totalPetrol;
document.getElementsByName("plastic")[0].value = totalPlastic;

Comments

0

If your controls are in a form, like:

<form>
  <input type="text" name="carbon">
  <input type="text" name="tree">
  <input type="text" name="petrol">
  <input type="text" name="plastic">
  ...
</form>

then you can get a reference to the form and access them as named properties of the form, e.g.

var form = document.forms[0];
form.carbon.value = totalCarbon;
form.tree.value = totalTree;
...

Just make sure you don't give form controls a name that is the same as a form property, like submit or name, as these will shadow the form's default properties of the same name (so you can't call form.submit() or access the form's name, if it has one).

Comments

0
//globally i declared carbon, tree, petrol, plastic
  document.getElementById("carbon").value   = carbon ; 
  document.getElementById("tree").value     = tree ; 
  document.getElementById("petrol").value   = petrol ; 
  document.getElementById("plastic").value  = plastic ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.