0

Just now my code is:

data = {'field_name':"some value",'name':$(this).val()};

However, I'd like to change the name key so it is dynamic, e.g something like:

var name = $(this).attr('id');
data = {'field_name':"some value",name:$(this).val()};

Is that possible?

1

3 Answers 3

2

To do this use the indexer syntax

var data = { 'field_name': "some value" };
data[name] = $(this).val();
Sign up to request clarification or add additional context in comments.

Comments

2

jQuery is not required for this:

data = {'field_name':"some value"};
data[this.id] = this.value;

Comments

0

This should do the trick:

var val = data.name;
delete data.name;
data[name] = val; //name is the identifier here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.