0

As you see I've link and attribute so I want to post my attribute with ajax like this:

all attribute value that begins with "data-record-*":

function getContent(par1, data) {

  $.ajax({
    url: par1,
    method: "post",
    success: function(data) {

    }
  });
}


$("a").on("click", () => {
  var getLink = $(this).attr("href");
  const data = $(this).data();
  const postData = {};
  Object.keys(data)
    .filter(key => key.startsWith("record"))
    .forEach(key => {
      postData[key.replace("record", "")] = data[key];
    });
  getContent(getLink, postData);
});
<a href="#" data-record-id="1" data-record-name="section-1">Click on me!</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

if I click anchor all data-record-* must be post with jquery ajax I want this how to do this ?

ani_css
  • 2,118
  • 3
  • 30
  • 73

2 Answers2

0

You may prepare the data object like:

$('a').on('click',function() {
  var data = {};

  data.record_id   = $(this).data("record-id");
  data.record-name = $(this).data("record-name");

  getContent("some_url", data);
});

Then write the function something like:

function getContent(par1, data){
 $.ajax({
        url: par1,
        method: "post",
        data: data,
        success: function(data) {

        }
    });
}
Abhi
  • 4,123
  • 6
  • 45
  • 77
  • no I mean begin width `data-records` automatically.for example I added `data-record-blabla` later..it must loop it too – ani_css Jun 29 '17 at 07:57
  • Oh ! my bad I got it wrong. Can you see if this answer helps https://stackoverflow.com/a/36972181/2968762 – Abhi Jun 29 '17 at 08:01
0

If I understood correctly, you want to make an ajax call on each element with a data-records-* attribute when you click an a element:

$('a').on('click',function(){
    $('[data-records-*]').each(function() {
        $.ajax({
            url: par1,
            method: 'post',
            success: function(data) {}
        })
    });
});

Not sure what is the purpose of doing this or how you use each data-records-* element in the ajax call or where that par1 comes from, but this is how you do what you're asking.

dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • Actually, that makes a request per every `data-record-*` attribute. I think the OP meant sending the ajax request with all the values. – mingos Jun 29 '17 at 08:21
  • @mingos hmm well I though that's what OP he was asking, he's not explaining him/herself very clearly, to be honest – dabadaba Jun 29 '17 at 08:22
  • True, though sending everything in a single call seems like a reasonable assumption. – mingos Jun 29 '17 at 08:24
  • Hi, my ajax template was an example.I just only want to post my all data-records-* value attribute with ajax and I don't know what am I write url section instead of par1 but thank you – ani_css Jun 29 '17 at 08:25