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?
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.
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.
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']); } }); });
.data() is what I would use. You can do:
$(header).data({"col":"DATA", "sort":"OTHERDATA"});
or
$(header).data("col","DATA");
$(header).data("sort","OTHERDATA");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With