How to append a value to input if empty or add a comma with value using ajax?

3053 views javascript
3

I have followed this answer and this is the code have:

ajax receiver:

    success: function(data) {
      jQuery("#assigncat").val(function(i,val) { 
        return val + (val ? '' : ', ') + data.cat_id;
      });

But I get:

230231

<input id="assigncat" value="230231">

While I should get: 230,231

<input id="assigncat" value="230,231">

answered question

@KrishnaJonnalagadda a number

1 Answer

13

You've got the two return values of the ternary operator mixed up. It should be:

return val + (val ? ', ' : '') + data.cat_id;

posted this

Have an answer?

JD

Please login first before posting an answer.