Silvertail Silvertail - 1 year ago 79
HTML Question

Selecting element by custom data

I have an html like so:

<div mydata style="..." class="..."></div>

I want to be able to select all divs that have mydata as an attribute so I can work with them in jquery. I've tried
but that doesn't seam to find anything.
Any thoughts?

Answer Source
$('div[mydata]').each(function() {
// `this` is the div
// $(this)

or if u set a value to the attribute u can fetch them by

$('div[mydata="value"]').each(function() {
// `this` is the div
// $(this)


This code snippet finds all of them.

 <div adam='l'>geh</div>
 <div adam='j'>asdad</div>
 <div adam='t'>hsad</div>
 <div adam='l'>hessssj</div>
 <div adam='tl'>hej</div>

    $('[adam]').css({ 'background':'red' });