Malin Dharmaratne
Malin Dharmaratne

Reputation: 5

Jquery -Combining string & Variable

I am new to jQuery and I cant seem to get the following code working..

for ( var i = 0; i < 2; i++ ) {
  $status[i] = $('select[name="status'+ i +'"] option:selected').val();
  $odd_a[i] = $("input:text[name='odd_a"+ 1 +"']").val();
  $odd_b[i] = $("input:text[name='odd_b"+ 1 +"']").val();
  $term[i] = $("select[name='term"+ 1 +"'] option:selected").val();
  $dh_place[i] =  $("input:text[name='dh_place"+ 1 +"']").val();
  $dh_total[i] = $("input:text[name='dh_total"+ 1 +"']").val();   
}   

I have several text boxes "status1, status2, status3 etc. I need to call their name by the for loop. If I replace the "i" with the "1" it works. I cant seem to call the variable "i" at that position.

Upvotes: 0

Views: 73

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

One problem I can see is the i starts with 0 where as your input starts with 1, so the first loop will not return any elements.

for (var i = 0; i < 2; i++) {
    $status[i] = $('select[name="status' + (i + 1) + '"]').val();
    $odd_a[i] = $("input:text[name='odd_a" + (i + 1) + "']").val();
    $odd_b[i] = $("input:text[name='odd_b" + (i + 1) + "']").val();
    $term[i] = $("select[name='term" + (i + 1) + "']").val();
    $dh_place[i] = $("input:text[name='dh_place" + (i + 1) + "']").val();
    $dh_total[i] = $("input:text[name='dh_total" + (i + 1) + "']").val();
}

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try with

  $status[i] = $('select[name="status'+ i +'"]').val();

and You need to start i value from 1 like

for ( var i = 1; i < 2; i++ ) {

Upvotes: 1

Related Questions