Rstdevelpzz
Rstdevelpzz

Reputation: 504

How to get the data-href value of a tag after modal is clicked in Laravel

i have a modal which opens when the Edit button is clicked now i want that the value of data-href in with which the modal opens on clicking to echo inside of modal body how can i do that

<a href="javascript:void(0);" data-href="{{ url('admin/product_edit/'.$cat->id) }}" class="edit_product btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal"> Edit</a>

Upvotes: 2

Views: 2066

Answers (3)

Shubham Srivastava
Shubham Srivastava

Reputation: 1877

You can do it like

<a href="javascript:void(0);" data-href="/sample/url" class="edit_product btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal" onclick='process(this)'> Edit</a>

function process(inp) {
    console.log(inp.getAttribute('data-href'));
}

Upvotes: 1

Kurt Chun
Kurt Chun

Reputation: 421

if you are using jQuery, then you can do this

let hrefVal = null

$('.edit_product').click(function () { hrefVal = $(this).data('href') })

Upvotes: 0

Always Helping
Always Helping

Reputation: 14570

You can use event.relatedTarget function have more dynamic approach to get the data-attributes.

You do not need to use an inline onclick function here this can be done easily using the native jQuery bootstrap function like show.bs.modal and get the a data attributes on click and to be displayed in the modal.

$("#editModal").on('show.bs.modal', function(event) {
  var button = $(event.relatedTarget) //Button that triggered the modal
  var getHref = button.data('href') //get button href
  $('#href').text(getHref) //show in the modal
})

Live Working Demo:

$("#editModal").on('show.bs.modal', function(event) {
  var button = $(event.relatedTarget) //Button that triggered the modal
  var getHref = button.data('href') //get button href
  console.log(getHref) //{{ url('admin/product_edit/'.$cat->id) }}
  $('#href').text(getHref) //show in the modal
})
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<a href="javascript:void(0);" data-href="{{ url('admin/product_edit/'.$cat->id) }}" class="edit_product btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal"> Edit</a>

<!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <span id="href"></span>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions