Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store 2 parameters with JQuery.data()

I have the following js:

$('.overview_table_header').click(function() {
  header = $(this)
  $.get("/sort", { col: $.trim($(this).text()), sort: header.data('sort') },
    function(data) {
      $('#pages').html(data.html);
      header.data('sort', data.sort);
    }
  );
});

Which passes 2 parameters (A get request to /sort): {"col"=>"DATA", "sort"=>"OTHERDATA"}

I'm new to JQuery and Ajax. How do I store The above DATA and OTHERDATA in a hidden field tag within my html? Is using JQuery.data() the best method to accomplish this task?

like image 988
JZ. Avatar asked May 12 '11 19:05

JZ.


People also ask

How can we store multiple values in a variable in jQuery?

What I get from your question you wan't to store key and value in different data-attributes . it will add a data-attribute which has a value 12 and can be retrieved using key 'a' like bellow. If you want to insert multiple data-attributes you have to set the data attribute for each item once.

Can you describe how jQuery stores data related to an element?

Basically jQuery holds the information you store/retrieve with data(name, value)/data(name) and remove with removeData(name) in an internal javascript object named cache . The rest is just a bit of javascript magic to make it work and keep all the associations right.

How can I get multiple data from Ajax in PHP?

php', data: 'c_id='+ $(this). val(), dataType: 'json', // jQuery will expect JSON and decode it for you success: function(reply_data){ $('#course_name'). val(reply_data['c_name']); $('#course_credit'). val(reply_data['c_credit']); } }); });


1 Answers

.data() is what I would use. You can do:

$(header).data({"col":"DATA", "sort":"OTHERDATA"});

or

$(header).data("col","DATA");
$(header).data("sort","OTHERDATA");
like image 164
Ryan Olds Avatar answered Oct 12 '22 11:10

Ryan Olds