Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

88
Views
Select elements by HTML5 data attribute in jQuery

Is it possible to select elements in jQuery by their HTML5 data attributes (for example, all <div> with data-role='footer')?

8 months ago · Santiago Trujillo
3 answers
Answer question

0

You can select on a data- attribute like any other attribute...using an attribute selector. In this case you want the attribute-equals selector, like this:

$("div[data-role='footer']")

They are handled specially in consumption by jQuery, e.g. allowing .data() to fetch from them with correct typing...but as far as DOM traversal goes, they're just another attribute, so think of them as such when writing selectors.

8 months ago · Santiago Trujillo Report

0

$('div[data-role="footer"]')

This simply uses the the attribute-equals-selector(docs).

There are several attribute selectors you can use (among the others).

8 months ago · Santiago Trujillo Report

0

jQuery Mobile recomends the $.fn.jqmData(), for example : $("div:jqmData(role='footer')")

Source: http://jquerymobile.com/test/docs/api/methods.html

8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.