Redeem date-id from a menu

Asked

Viewed 126 times

3

How do I pass the data-id of a menu link to a modal? For example, I have a menu with the link1 - Link2 - link3. For example, clicking on Link2 opens a modal displaying the values of Link2. These values are rescued from the database, so I need to know what the date-id of the menu to pull from the database the information corresponding to that data-id.

  • You have to put the code you already have... otherwise it’s hard to help.

1 answer

0

To get the ID you can use a Javascript function... But you don’t necessarily need to use a Data attribute since you have the ID attribute. Then you can do whatever you want with it.

$(document).ready(function() {
  $('ul.menu > li').on('click', function() {
    var dataId = $(this).attr('id').replace('link-', '');

    console.log(dataId);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="menu">
  <li id="link-1"><a href="#">Link 1</a>
  </li>
  <li id="link-2"><a href="#">Link 2</a>
  </li>
  <li id="link-3"><a href="#">Link 3</a>
  </li>
</ul>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.