Since you are concatenating numbers on to a string, the whole thing is treated as a string. When you want to add numbers together, you either need to do it separately and assign it to a var and use that var, like this:
i = i + 1;
divID = "question-" + i;
Or you need to specify the number addition like this:
divID = "question-" + Number(i+1);
EDIT
I should have added this long ago, but based on the comments, this works as well:
check it here, it's a JSFiddle
Since you are concatenating numbers on to a string, the whole thing is treated as a string. When you want to add numbers together, you either need to do it separately and assign it to a var and use that var, like this:
Or you need to specify the number addition like this:
EDIT
I should have added this long ago, but based on the comments, this works as well:
Use this
+
operator behave asconcat
that's why it showing 11.Simple as easy ... every input type if not defined in HTML is considered as string. Because of this the Plus "+" operator is concatenating.
Use parseInt(i) than the value of "i" will be casted to Integer.
Than the "+" operator will work like addition.
In your case do this :-
Use this instead:
It's a fairly common problem and doesn't just happen in JavaScript. The idea is that
+
can represent both concatenation and addition.Since the + operator will be handled left-to-right the decisions in your code look like this:
"question-" + i
: since"question-"
is a string, we'll do concatenation, resulting in"question-1"
"question-1" + 1
: since"queston-1"
is a string, we'll do concatenation, resulting in"question-11"
.With
"question-" + (i+1)
it's different:(i+1)
is in parenthesis, its value must be calculated before the first+
can be applied:i
is numeric,1
is numeric, so we'll do addition, resulting in2
"question-" + 2
: since"question-"
is a string, we'll do concatenation, resulting in"question-2"
.You may also use this
to be sure that
i
is converted to integer.