Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error ' missing : after property id' when using Jquery ajax function

In the following code, I'm trying to send a key-value pair and I always get the error:
" missing: after property id "

$(".general").change(function () {
  fields = { $(this).attr('id') : "1" };
  $.ajax({
   type: "POST",
   url: "ajax/update_general.php",
   data: { fields: fields },
   dataType: "json",
   });
})

I've figured that what causes the problem is:

$(this).attr('id')

But I have no clue why. I've tried to first assign $(this).attr('id') to a variable, and put the variable in the ajax call, but that didn't help. How can I fix that?
Thank you!

like image 996
Israel Avatar asked Aug 12 '26 09:08

Israel


1 Answers

It's a syntax error. You can't use the return value of a function call as a property name.

You can, however, use that return value in bracket notation after initializing the object:

  fields = {};
  fields[$(this).attr('id')] = '1';
like image 101
BoltClock Avatar answered Aug 14 '26 23:08

BoltClock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!