Skip to content

How can I get data-id from multiple elements with single onclick?

as the title says I have multiple buttons which they have data-id. I want to open fancybox modal by getting the clicked buttons data-id. Each element on click will open their own modal. I can’t use class selector because this function is attached to somewhere else, and I know that $(this) here doesn’t mean clicked element.

Thank you for your help.

JQuery

  function get_cookie_param(){
    function lorem() {
      var dataId = $(this).data("lorem");
      alert(dataId)
      $.fancybox.open({
        src: dataId,
        type: "iframe",
        opts: {
          toolbar: false,
          smallBtn: true,
          iframe: {
            preload: false,
          },
        },
      });
  }
  ...
}

HTML

<a href="#" onclick="get_cookie_param();" data-lorem="products/camera.html" class="btn-lorem"></a>

Answer

function get_cookie_param(ele) {
  var dataId = $(ele).attr("data-lorem");
  alert(dataId);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="#" onclick="get_cookie_param(this);" data-lorem="products/camera.html" class="btn-lorem">Element</a>