0

split_values1 is an array and it contents some values, all i want to do is add those values to the textfield with a , separating them.

for(var i = 0; i < split_values1.length; i++) {
   $('#teach_subjects').value += split_values1[i]", ";
}
2
  • possible duplicate of Append text to input field Commented Feb 16, 2012 at 13:42
  • Why do you think string concatenation is performed with &? jQuery objects don't have a value property. jQuery's documentation os quite good, it's the first thing you should have a look at: api.jquery.com Commented Feb 16, 2012 at 13:42

4 Answers 4

4

Use join on your array and val to set textfield value:

$('#teach_subjects').val(split_values1.join(', '));
Sign up to request clarification or add additional context in comments.

1 Comment

@JackalopeZero Just call me Lucky Luke :)
1

Just do this:

$('#teach_subjects').val(split_values1.join(", "))

Comments

1

Try:

var value = split_values1.join(", ");
$('#teach_subjects').val(value);

Comments

0

Since you are using jQuery I would do this.

var val = $('#teach_subjects').val();
for(var i = 0; i < split_values1.length; i++) {
   val += split_values1[i] + ", ";
}
$('#teach_subjects').val(val);

It's a bad idea for performance reasons to constantly update a value in the DOM, so I used string concatenation.

As others have stated join might be a better choice.

4 Comments

Superb, thanks! I know it was a simple one for you, but i am new to using javascript! Thanks again.
-1 : There will be a trailing ', ' at the end of the textfield.
@sinsedrix - what if they need it?
@DanielA.White The OP asked for a separator, not an end of value character.

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.