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!
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: