8

I am trying to update one hidden input field with another visible input field using javascript...

but my code doesn't work for some reason!!

This is my javascript:

<script language="javascript">
    function process1() {
        document.getElementById("sum_total").value = (document.getElementById("my-item-price").value);
    }
</script>

And This Is My form code:

<form><input name="A1" type="text" class="A1" id="A1"/></form>

<form><input type="hidden" id="A2" name="A2" value="process1() "/></form>

Update

<script language="javascript"> 
    function process1() { 
        document.getElementById("sum_total").value = (document.getElementById("my-item-price").value); 
    } 
</script>
8
  • 2
    No input has the ID of sum_total or my-item-price, try changing those to A1 and A2 Commented Feb 5, 2013 at 14:21
  • sorry, I forgot to update the javascript code. This is my javascript but still doesn't work!! <script language="javascript"> function process1() { document.getElementById("sum_total").value = (document.getElementById("my-item-price").value); } </script> Commented Feb 5, 2013 at 14:21
  • yes, sorry. i've changed it to A1 and A2 but still doesn't work!! Commented Feb 5, 2013 at 14:22
  • Erm, still trying to understand the logic. ~_~ Commented Feb 5, 2013 at 14:23
  • I can't even edit the question now!! I have changed the IDs accordingly but still didn't work!! Commented Feb 5, 2013 at 14:23

6 Answers 6

11

The code you have is bad. You haven´t got ids you are looking for in javascript.

Try this:

 <script language="javascript">
 function process1(showed) {
    document.getElementById("A2").value = showed.value;
}
</script>

<form><input name="A1" type="text" class="A1" onChange="process1(this)"/></form>

<form><input type="hidden" id="A2" name="A2" value="5" /></form>
Sign up to request clarification or add additional context in comments.

1 Comment

Fix the typo in onChange.
4

I think the most reliable will be to use onKeyUp rather than onChange or mouse events (onBlur etc.). The following updates the hidden field as each character is entered (I have un-hidden A2 so you can see what is happening).

<html>
<head>
<script language="javascript">
function process1() {
    document.getElementById("A2").value = (document.getElementById("A1").value);
    }

</script>
</head>
<body>
<form><input name="A1" type="text" class="A1" id="A1" onKeyUp="process1()" /></form>
<form><input id="A2" name="A2" value="" /></form>
</body>
</html>

1 Comment

your suggestion was clear and helpful. Thank you for your help bud.
1

You should bind an event to update the value. For example onkeyupevent document.getElementById("A1").onkeyup=function() { document.getElementById("A2").value = this.value };

Comments

1

Try this: Live Demo

HTML

<form><input name="A1" type="text" class="A1" id="A1"/></form>

<form><input type="hidden" id="A2" name="A2" value=""/></form>

<button onClick="process1();">Update</button>

JS

function process1() { 
    document.getElementById("A2").value = document.getElementById("A1").value; 

    alert(document.getElementById("A2").value);
} 

4 Comments

plz delete this answer, the asker stated in comments that it's a typo and should be fixing it by now
guys I already said I've changed the IDs in my file accordingly but didn't work!! the above code is a mistake!
@ATOzTOA, it doesnt say anything. and i did try your Live Demo but that brings up a pop-up which is not what I am after!
@RoozFar The pop-up is displaying the value of the hidden input element. If the popup shows the value you entered, then your data is copied over.
0

I changed the value to blank and gave the input an onmouseout (you can use another type if you want) to execute the function and swap values.

<script language="javascript">
function process1() {
document.getElementById("A2").value = document.getElementById("A1").value;
}

</script>
</head>

<body>



And This Is My form code:

<form><input name="A1" type="text" class="A1" id="A1"/></form>

<form><input type="text" id="A2" name="A2" value="" onmouseout="process1()" /></form>

1 Comment

Thank you, I did try the onfocous mouse events and it didn't work so I thought the issue might be from elese where!! The A2 input is hdden so I can't use mouse events! or can I?
0
<script language="javascript">
function process1() {
    document.getElementById("A1").value = (document.getElementById("A2").value);
}
</script>
And This Is My form code:
<form><input name="A1" type="text" class="A1" id="A1"/></form>

<form><input type="hidden" id="A2" name="A2" value="process1() "/></form>
<div onClick="process1()">doit</div>

does work

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.