0

I have a table with form elements in it.

 for ($a = 0;$a<25;$a++)
 {
 ?>
 <tr><td>Number</td>
     <td class='ok'><input type='text' name='number[<?php echo $a;?>]' id='number[<?php echo $a;?>]'></td>
  <td id='error[<?php echo $a;?>]'></td></tr>
 }

i then send this data to jquery for ajax processing, and based on response if i get an error i want to fill the appropriate td (based on $a) with 'fix it'.

                      success:      function(result)
                    {

 //get the value of the $a value in form.

 var currentId = $(this).attr('id');
 var field1 = currentId.split("[");
 field2 = field1[1].split("]");
 fieldId = field2[0];

    if (result == "error")
    {
            $(this).removeClass('ok').addClass('error');
            var errorId = "error["+fieldId+"]";
            $("#"+errorId).html('fix it');
    }

I am getting the class to change, so i know the result is "error". my problem is $('#'+errorId) is not being populated with "fix it".

bart2puck
  • 2,432
  • 3
  • 27
  • 53
  • Is `$("#"+errorId)` an actual element? If you do `$("#"+errorId).length` what is the result? – tymeJV Oct 02 '13 at 18:21
  • i get a length of 0. so the id of the element is number[3]. it has a matching error td. error[3]. if result of element 3 is error, i am trying to populate td id error[3]...make sense? – bart2puck Oct 02 '13 at 18:26
  • Makes sense, the selector is simply not finding the element. Does `errorId` have some spaces in it possibly? – tymeJV Oct 02 '13 at 18:27
  • I suspect `[` and `]` have a special meaning to jQuery's selector. If you replace `id="error[]"` with, say, `id="error_"` it might well work. (And change the line where you write to it too, obviously!) – Clart Tent Oct 02 '13 at 18:29
  • 1
    It looks like it might be the square brackets in the selector, see this question: http://stackoverflow.com/questions/1239095/find-dom-element-by-id-when-id-contains-square-brackets – Clart Tent Oct 02 '13 at 18:32

1 Answers1

0

You can do something like this;

if (result == "error") {
        $(this).removeClass('ok').addClass('error');
        $(this).parent().find("td[id$=errorId]").html('fix it');
}