Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you make ajax data key dynamic in jquery?

Tags:

jquery

ajax

I'm trying to make my inline edit to be dynamic so it will just depend on some data- attributes from my markup so here's the code for now:

$(".inline-edit").editable(
  function(value, settings) {
    var editableField = $(this);

    $.ajax({
          type: 'PUT',
          url: editableField.attr('data-href'),
          dataType: 'html',
          success: function(html) {
            editableField.parents('.replaceable').replaceWith(html);
          },
          data: { 'regression_test_environment[name]' : value }
        });
        return(value);
  },
  {
    event: 'click',
    width: '80%',
    height: '20',
    submit : 'OK'
  }
)

i want the name in regression_test_environment[name] to be editableField.attr('data-column-name') but it always fails in compiling because it keeps taking the key as a string. I tried making a variable after the editable field variable assignment and building the string as a different variable but it doesn't want to evaluate the key as a function.

Is there a way to do this? or am i stuck in creating a separate .editable call for each of my editable fields?

like image 807
corroded Avatar asked Mar 11 '11 08:03

corroded


People also ask

How can show dynamic data in jQuery?

In this example, we should go with the following steps to implement dynamic data loading using jQuery. Create UI to be appended for each user information. Download and import latest version of jQuery library files and jQuery UI files. Create custom jQuery handlers to append UI and load dynamic data into it.

How does AJAX work in jQuery?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

What dataType property of AJAX () method specifies?

The dataType option specifies the type of response data, in this case it is JSON. The timeout parameter specifies request timeout in milliseconds. We have also specified callback functions for error and success. The ajax() method returns an object of jQuery XMLHttpRequest.

What is pushState AJAX?

The pushState function is responsible for changing the browser's URL without initiating requests back to the server. The pushState function is responsible for changing the browser's URL without initiating requests back to the server.


1 Answers

Better, less confusing answer:

var data = {};
data[thisField] = $(this).text();

$.ajax({
    data: data
});
like image 126
keithhackbarth Avatar answered Oct 30 '22 11:10

keithhackbarth