2

I have an <input> in my document. There I want the user to do some input.

var myVar = document.getElementById('userInput').value;

Then I want this value to be shown in another <input>.

document.getElementById('Preview').value = myVar;

This code somehow doesn't work.

Can anybody help?

Thanks in advance!

3
  • 1
    Do you want the value to be shown on another input when the user type? Commented May 19, 2011 at 13:18
  • 1
    Can you show the HTML that your JavaScript is referencing? Commented May 19, 2011 at 13:20
  • 1
    Try to add alert("message"); in your code. Do you get this message when your code is running? Commented May 19, 2011 at 13:24

3 Answers 3

11

Update based on further information in comments before:

<button onClick="calculateThatObscenityDeleted()">"Save"</button >

Submit buttons will submit forms, thus running the JS but immediately blanking the form.

(That might still not be the actual issue, the question is still missing most of the code)


Original answer before it was revealed that the question didn't reflect the problem:

var myVar=document.getElementById('userInput').value;

Don't forget the =.

(And, obviously, you need to use that code in an event handler so it isn't executed only when the document loads and before the user has typed anything.)

Sign up to request clarification or add additional context in comments.

6 Comments

I actually didn't forget the "=" in my code. And this stuff is handled in a onClick()-function.
@bodycountPP: then you’ll need to show us more of the code for us to be able to figure out why it isn’t working for you.
Then your reduced test case (a) Reduced away the problem and (b) Added extra problems. Try providing a test case that demonstrates the entire problem. Debugging through a keyhole isn't fun.
<button onClick="calculate()">"Save"</button > : But it's solved now. The problem was: onclick() instead of onClick(). Thank you guys!
No, that wasn't the problem. Attribute names are case insensitive in HTML documents as must be lower case in XHTML documents. Changing from lower case to camelCase can't possibly solve it.
|
1

Is it form? Try something like this:

oFormObject = document.forms['myform_id'];
oFormObject.elements["element_name"].value = 'Some Value';

Comments

1

Besides the typo, you have to bind an event handler to the first <input>:

var input_a = document.getElementById('userInput');
var input_b = document.getElementById('Preview');

input_a.onkeyup = function(){
    input_b.value = input_a.value;    
}

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.