715 posts
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • 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
  • 5 Years of Membership
  • Affiliate Level 2
  • Author Level 5
  • Collector Level 2
+8 more
wizylabs
says

you could do it with jQuery, something like below

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

    return ID;    
});
715 posts
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • 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?

1095 posts Best-dressed man at PressNomics 2013
  • Power Elite Author
  • Author Level 12
  • Featured Author
  • Featured Item
+13 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
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • Belgium
+1 more
Hy-Studio
says

Okay cool thanks, I’ll try it out.

715 posts
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • Belgium
+1 more
Hy-Studio
says

Great, it worked! thanks :)

2952 posts
  • 6 Years of Membership
  • Affiliate Level 5
  • Author Level 8
  • Bundle Boss
+12 more
DDStudios
says

What’s YUI 2 ?





(that’s supposed to be sarcastic)

715 posts
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • 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