4

how can i transfer the Value from Input 1 in 2 and add some letters?

<script type="text/javascript">

function doit(){
document.getElementById('input2').value=document.getElementById('input1').value;
}

</script>

Input1: 2342

Input2: pcid2342d

Can some one help me?

3
  • Your attempt looks good. Does it not work? Commented Jul 1, 2010 at 13:20
  • The code appears to be mostly correct. What errors are you getting? What is not working? Commented Jul 1, 2010 at 13:21
  • there are no errors, it was that i don't know how to add letters on the string :-) Commented Jul 1, 2010 at 13:32

4 Answers 4

7

Just use the + operator to add a string before and after the input value.

<script type="text/javascript">

function doit(){
document.getElementById('input2').value="pcid" + document.getElementById('input1').value + "d";
}

</script>
Sign up to request clarification or add additional context in comments.

Comments

1

String concatenation:

document.getElementById('input2').value = "stuff" + document.getElementById('input1').value + "other stuff";

When dealing with numbers you could start by concatenating with empty string to avoid adding numbers together instead of concatenating to strings (because of operator evaluation order):

document.getElementById('input2').value = "" + 1234 + 567 + document.getElementById('input1').value + 89;

Comments

0

Well you seem to have done the work already, all you need is something to click on to execute it:

<button onclick="doit()">click me</button>

1 Comment

i have an other input with onload so i don't need a button, but thanks^^
0

Why don't you try something in jQuery?

  $("#Anything").click(function() {
     $("#Field1").val($("#Field2").val());
  });

The "click" is just a assumption =)

1 Comment

For such a trivial task Jquery is overkill I think!

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.