1

How am I supposed to concatenate this?

here's my javascript code

var c0 = document.all.ntext.value; 
var c1 = document.all.stext.value;
var x;

for(x=0; x<2; x++)
{
    a.innerHTML = c //contatenation needed
}
3
  • 2
    why use looping? instead of a.innerHTML = c0 + c1; Commented Oct 15, 2012 at 8:55
  • 2
    Don't use document.all, it is a non-standard IE4ism. Commented Oct 15, 2012 at 8:55
  • 3
    Remember this: Everytime you want to use numbered variables it will be the right time to start learning about arrays Commented Oct 15, 2012 at 8:56

2 Answers 2

1
var c0 = document.all.ntext.value; 
var c1 = document.all.stext.value;
var x;

for(x=0; x<2; x++)
{
    a.innerHTML = c0 + с1
}

Is that what you want?

It'll be much better if you'll go this way:

var c = [document.all.ntext.value, document.all.stext.value];
var x;

for(x=0; x<c.length; x++)
{
    a.innerHTML += c[0];
}
Sign up to request clarification or add additional context in comments.

Comments

0
var needed = ['ntext', 'stext'];   
a.innerHTML = needed.map(function(key) {
  return document.all[key].value;
}).join('');

.map need a shim for old browsers.

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.