-1

I got 3 fields: name, last name and concat.

What I have to do is concat the first character of the field name with the last name. And as I write, the field Concat is changing.

I have this by now:

<p>Name</p><input type="text" id="name"  onChange="document.getElementById('concat').value=this[0].value;" />
<p>Last name</p><input type="text" id="last_name" document.getElementById('txt').value=document.getElementById('txt').value + this.value;"/>
<p>Concat</p>
<input type="text" id="concat"  />

But the first character doesn't seem to be working, I think the problem might be in this[0].value; or how do I get the first chatacter, is there any other function to do it?.

Thanks, beforehand.

5
  • what does your javascript look like? Commented Jan 2, 2013 at 19:35
  • Is in the code above inside html input tags: onChange="document.getElementById('concat').value=this[0].value;" Commented Jan 2, 2013 at 19:36
  • 1
    thats not a very good pattern. Commented Jan 2, 2013 at 19:37
  • Might I suggest addEventListener as a cleaner alternative to inline on* handlers? Commented Jan 2, 2013 at 19:40
  • Check this out. This might help you as well. stackoverflow.com/questions/2268245/… Commented Jan 2, 2013 at 19:44

3 Answers 3

0
this[0].value

If you want the first character of the input fields value, that's not what you should do.

this is the input field. this.value is the string you want the first character of. And this.value[0] will get you that strings first character.

So:

this.value[0]
Sign up to request clarification or add additional context in comments.

Comments

0

You made some mistakes, here's your corrected code

<p>Name</p><input type="text" id="name"    onChange="document.getElementById('concat').value=this.value[0];" />
<p>Last name</p><input type="text" id="last_name" onChange="document.getElementById('concat').value=document.getElementById('name').value[0] + ' '    + this.value;"/>
<p>Concat</p>
<input type="text" id="concat"  />​​​​​​​​​​​​​​​​​​​​​​

please compare with your own code!

Comments

0

You're using [0] on this in your First-Name textbox, but it should be on the actual value instead:

onChange="document.getElementById('concat').value=this.value[0];"

Alternatively, you could be explicit and use .substring() to get the first character:

onChange="document.getElementById('concat').value=this.value.substring(0, 1);"

This will get the first character of the input. Though a bit longer to write/read, it will convey your exact purpose.

Note: If your input is empty, you will get an undefined value inserted into your concat textbox when you use this.value[0] as, well, there is no [0]. Using .substring(0, 1) will give no such text (if it's a concern).

Additionally, your second text-box for Last-Name has two typos (based on your sample code) in that it doesn't contain the onchange= attribute and it uses an id txt instead of concat. Also, it doesn't attempt to take a substring of the first-name textbox.

Try updating your full block of code to the following:

<p>Name</p>
<input type="text" id="name" onchange="document.getElementById('concat').value=this.value.substring(0, 1);" />
<p>Last name</p>
<input type="text" id="last_name" onchange="document.getElementById('concat').value=document.getElementById('name').value.substring(0, 1) + this.value;"/>
<p>Concat</p>
<input type="text" id="concat"  />

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.