-1

I wish to be able to use data-id as an identifier on an action but am not sure how this is done. Usually for an id I would do the following:

$("#id").click.......

However I do not know the syntax to use data-id as the identifier. Something like this but not sure what:

$(data-id).click.....
EamonnMcElroy
  • 587
  • 6
  • 20
  • 1
    `$('[data-id=dataidvalue]')` check [attr selector](https://api.jquery.com/attribute-equals-selector/) for further infor – guradio Sep 20 '16 at 10:46
  • 1
    Possible duplicate of [jQuery selectors on custom data attributes using HTML5](http://stackoverflow.com/questions/4146502/jquery-selectors-on-custom-data-attributes-using-html5) – Legionar Sep 20 '16 at 10:50

1 Answers1

1

you can do it like:

$(document).ready(function() {
  $("[data-id='mydiv']").click(function() {
    alert($(this).text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div data-id="mydiv">click me div text</div>
vijayP
  • 11,432
  • 5
  • 25
  • 40