715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio says

Hello,

I have a DIV that has an ID and class, the class is fixed but the ID is generated dynamically.

Now I need to have the ID for a script, anyone knows how to get this by using for example the class?

Or does someone have a better idea? :)

Thanks!

1012 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+6 more
wizylabs says

you could do it with jQuery, something like below

$('.class').each(function() {
    var ID = $(this).attr('id');

    return ID;    
});
715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio says

Yeah sorry, forgot to mention that my client doesn’t use jQuery, he uses YUI 2 :(.

Does something like parentNode.id exists in plain javascript?

1045 posts Best-dressed man at PressNomics 2013
  • Has sold $1M+ on Envato Market and is now a Power Elite Author
  • Power Elite Author: Sold more than $1M on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
+10 more
Parallelus says

You can use standard JavaScript for getting the ID after referencing the item using YUI “getElementsByClassName”. Just be sure there are not multiple items with the same class name.

var theElement = YAHOO.util.Dom.getElementsByClassName('theClassName');
var theID = theElement[0].id;
alert(theID);
715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio says

Okay cool thanks, I’ll try it out.

715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio says

Great, it worked! thanks :)

2952 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 200+ members
  • Has sold $125,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+10 more
DDStudios says

What’s YUI 2 ?





(that’s supposed to be sarcastic)

715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio says

Lol yeah, I know, I really don’t like it …

But hey, the client pays me a lot to make things work, so … after this project I defenitly need another project where there’s jQuery and PHP , I’m working on a Java project now :crying:

by
by
by
by
by
by