0

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 $('[mydata]') but that doesn't seam to find anything. Any thoughts?

The Process
  • 5,913
  • 3
  • 30
  • 41
Silvertail
  • 169
  • 1
  • 13

1 Answers1

1
$('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)
});

EDIT

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>

$(document).ready(function(){
    $('[adam]').css({ 'background':'red' });
});
A.troed
  • 24
  • 3
  • This should work. so probably u have some problem elsewhere – A.troed Apr 29 '16 at 13:51
  • What if I want it to be on any element with a mydata value, not just a div? – Silvertail Apr 29 '16 at 14:22
  • using just [mydata] works great. I had a load sequence problem. For anyone else with this problem, make sure you stick this in a $(document).ready function or the selector may be called before the elements exist in which case they won't be found. – Silvertail Apr 29 '16 at 14:33
  • a tip. next time, send your code so people can see =) glad i could help ya – A.troed Apr 29 '16 at 14:39