0

Are there any more than just a Id and Class -selectors to work with when using Jquery?

For example:

<div id="myId" class="myClass"></div>

<script>
    $('.myClass').click(function() {
        alert(this.id); //alerts my "myId"
    });
</script>

I want something like:

<div id="myId" data-id="myDataId" class="myClass"></div>

<script>
    $('.myClass').click(function() {
        alert(this.dataId);
    });
</script>
user3228992
  • 1,373
  • 1
  • 16
  • 31
  • http://stackoverflow.com/questions/4146502/jquery-selectors-on-custom-data-attributes-on-html5 –  Sep 09 '14 at 00:00

3 Answers3

1

You can different selectors like:

$( "div[data-id='myDataId']" );

and then:

$(this).attr("data-id")

or .data() (if you use newer jQuery >= 1.4.3)

$(this).data("id")

You can read it here: http://api.jquery.com/attribute-contains-selector/ http://api.jquery.com/category/selectors/

codebased
  • 6,945
  • 9
  • 50
  • 84
1

You can do:

<div id="myId" data-id="myDataId" class="myClass"></div>

<script>
    $('.myClass').click(function() {
        alert($(this).data("id"));
    });
</script>
Mark Silverberg
  • 1,249
  • 2
  • 8
  • 21
0
 $("[data-id=myDataId]")

JSfiddle http://jsfiddle.net/sjj41box/1/