user8022208
user8022208

Reputation:

I need to concatenate two variables in jquery

I didn't find an answer to this, I need to concatenate two variables (ans & i) in Jquery to get ans1, ans2, etc. I tried this:

if(preguntaTipo<2){
        var numero = "";
    }
    else{
        var numero = $('#numero').val();
        for (var i = 1; i < numero; i++) {
            var ans.i = $('#ans'+i).val();
        }
    }

Its a mode to do like PHP $ans.$i? I also tried ans + i and it didn't work...

Upvotes: 0

Views: 175

Answers (1)

ErikE
ErikE

Reputation: 50191

var ans.i = makes no sense.

You can't have periods/dots inside of variable names.

Is this what you're looking for?

var ans = [];
for (var i = 1; i < numero; i++) {
    ans[i] = $('#ans' + i).val();
}

You could also use ans.push($('#ans' + i).val()); which would make the resulting answer array 0-based instead of 1-based.

Upvotes: 1

Related Questions