0

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.

3
  • Check the quotes signs. Commented Nov 1, 2013 at 11:01
  • Is there a specific reason you need to do this manually? $('form').serialize() would seem an easier alternative. Commented Nov 1, 2013 at 11:04
  • 1
    If i < 2, how can you possibly get any other value than 0 or 1? Commented Nov 1, 2013 at 11:05

2 Answers 2

1

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++ ) {
Sign up to request clarification or add additional context in comments.

Comments

0

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();
}

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.