0

I'm having problems with dynamically adding a row to a table using data stored in two arrays (categories and treatments). The arrays are fine, I've determined that.

When passing just the categories array the new row displays but the select box reads [object:object], it's clearly blank.

When I pass a second array with it, as shown below, the console reads 'undefined is not a function'.

Any help would be hugely appreciated!

 // Add an extra row when button is clicked
 var counter = 1;
   $('input.add').click(categories, treatments, function(){
      counter++;      

      var newRow = '<tr><td><label for="category' + counter + '">Category</label></td><td><select id="category' + counter + '" name="category' + counter + '" required="required">';      

      $.each(categories, function(key, value) {   
      $('#category' + counter)
         newRow += '<option value ="' + key + '">' + value + '</option>';
      });

      newRow += '</select></td><td><label for="treatment' + counter + '">Treatment</label></td><td><select id="treatment' + counter + '" name="treatment' + counter + '">';

      $.each(treatments, function(key, value) {   
      $('#treatment' + counter)
         newRow += '<option value ="' + key + '">' + value + '</option>';
      });    

      newRow += '</select></td></tr>';     

      $('table.treatments').append(newRow);
   });
});
1
  • 1
    Please create a JsFiddle with the problem statement. Commented Aug 18, 2014 at 17:45

1 Answer 1

1

The first parameter for the jQuery .click() is an Object, and you're trying to pass two arrays.

This should work for you (remember to check for the missing semi-colons):

// Create an Object obj containing the two arrays.
$('input.add').click(obj = { categories: categories, treatments: treatments }, function () {
    counter++;

    var newRow = '<tr><td><label for="category' + counter + '">Category</label></td><td><select id="category' + counter + '" name="category' + counter + '" required="required">';

    // Use the obj.
    $.each(obj.categories, function (key, value) {
        $('#category' + counter);
        newRow += '<option value ="' + key + '">' + value + '</option>';
    });

    newRow += '</select></td><td><label for="treatment' + counter + '">Treatment</label></td><td><select id="treatment' + counter + '" name="treatment' + counter + '">';

    // Use the obj.
    $.each(obj.treatments, function (key, value) {
        $('#treatment' + counter);
        newRow += '<option value ="' + key + '">' + value + '</option>';
    });

    newRow += '</select></td></tr>';

    $('table.treatments').append(newRow);
});

Demo

jQuery .click()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, that makes sense to me. The JFiddle you put up works perfectly, bizarrely though when copying and pasting the code I still have the same problem, the input box is showing [Object, object]. I have added a console log line to the function and it displays perfectly on the console, I can't understand why its not working properly
SOLVED - it was due to the structure of the arrays I believe as changing value to value.category did the trick. Thanks for your help, very much appreciated!

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.