Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Caffeine - Nicotine - Sugar
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Germany, Cologne
    Posts
    60
    Rep Power
    14

    Question [JavaScript] Get all elements of class 'abc'


    Hello ppl,

    how can i get all elements of an html-page which have the attribute class="abc" in an array? I need it for IE 4+, NC 6+.

    TIA
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0
    Sorry for digging this up but I thought this might be useful for visitors who have searched through google to here.

    Time for the modern way.

    Use jQuery.

    And what you asked can be achieved by simply $('.abc'). That's it!
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    Originally Posted by ichsie
    Sorry for digging this up but I thought this might be useful for visitors who have searched through google to here.

    Time for the modern way.

    Use jQuery.

    And what you asked can be achieved by simply $('.abc'). That's it!
    Time for stupid and lazy way.

    There is no need for JQuery nor any other framework for that. There is no need for frameworks at all most of the time. Do you think JQuery uses other language than javascript?

    First at all, the modern browsers have already implemented the getElementsByClassName() method. Except, of course , IE, for which we can use this kind of approach (or variants, if you like to play with)
    Code:
    onload=function(){
    if (document.getElementsByClassName == undefined) {
    	document.getElementsByClassName = function(className)
    	{
    		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
    		var allElements = document.getElementsByTagName("*");
    		var results = [];
    
    		var element;
    		for (var i = 0; (element = allElements[i]) != null; i++) {
    			var elementClass = element.className;
    			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
    				results.push(element);
    		}
    
    		return results;
    	}
    }
    }
    now use : document.getElementsByClassName('abc')

    Isn't that simpler than loading hundreds of KB of JQuery heavy code lines?

    Comments on this post

    • Winters agrees : Very efficient code. Did you write it?
    • lnxgeek agrees : Too bad it took this long for this thread to get interesting.
    • ChiefWigs1982 disagrees
    • elstcb agrees : That is fantastic code, thank you so much.
    Last edited by KorRedDevil; January 3rd, 2009 at 03:18 AM.
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    To Winters:

    Actually the code is a result of 3 minds. I wrote the principle, some other coder, Jeff Mott, has improved the RegExp() object, and another guy, called himself rnd me, has pointed out that the indexOf() method is faster than a regexp test.

    It is not the only way to do that, but it is one of the fastest, I think.
  10. #6
  11. Prisoner of the Sun

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,309
    Rep Power
    2351
    Some more alternatives:

    http://stackoverflow.com/questions/2...ific-css-class
    http://code.google.com/p/getelementsbyclassname/
    http://www.robertnyman.com/2008/05/2...ame-anno-2008/

    Synchronicity... I just found stackoverflow.com earlier today and now I find another article from them whilst investigating this thread. Interesting site.
    .
    :: My blip.fm tunes :: Web Design Feeds :: Web Dev Feeds :: CheatSheets :: PHP :: MySQL :: 13 Moon FB App.

    "All matter is merely energy condensed to a slow vibration. We are all one consciousness experiencing itself - subjectively. There is no such thing as death, life is only a dream. We are the imaginations of ourselves."
    - Bill Hicks


    "Truth is hidden in the subtle nature of the heart of everything, although it is invisible. One cannot see it from inside and neither from the surface. One can only live and experience it."
    - Heart Sutra
  12. #7
  13. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    Reading the stackoverflow comments I saw again the invitation for using JQuery, which shows me that people really don't understand what JQuery and other frameworks are and do.

    That $() JQuery method is a custom one. It returns an element or a collection of elements with an single id or with common name, classname, attribute... At the basis, JQuery's heavy function uses the same javascript principle I have used - as JQuery is nothing but a javascript library. But it does not worth to use frameworks/libraries for common javascript tasks, and common javascript tasks are 99% of our developing tasks.

    I dare to say that my variant of crossbrowser getElementsByClassName() is faster that JQuery's and easy to understand.

    You may find in detail my position regarding frameworks at:
    http://forums.devshed.com/javascript-development/537966-ajax-library-gwt-dojo-dwr-post2063621.html
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0
    Time for stupid and lazy way.
    ...
    Yeah well, your way makes sense for a conceited nerd coder who thinks he knows everything javascript than people who'd just like to make things quicker and simpler with a widely tested / accepted production level framework.

    Packed jquery (for production delivery) is merely 30.3kb which is no more than 1/4 of most of the photos hosted at flickr.com. If I could just achieve this simple task with only 9 characters by the help of jquery why should I spend hours or even days of time learning your geeky way? I don't think there's anything glorious about it. 0.6KB of js code for just selecting some classes? Well, your code is truly holy simpler.

    Plus, with jquery I can go far far away than what I could personally write rather than just selecting class 'abc' - this, is where I suggest Pushkin be heading, using jquery from now on for everything he needs from javascript. It's just a beginning. However, if everyone thinks it's cool to write everything themselves from scratch, here'd be full of people who call others stupid for striving for efficiency.

    Just ask yourself, is that the code you'd personally use for your own projects? I highly doubt it. That's the difference of being helpful and being a showoff in a thread.

    You can be proud of it, me too sometimes, actually. But please, stop being arrogant and stop calling other people stupid for this. That's what's stupid.

    Comments on this post

    • ChiefWigs1982 agrees
    • f_nietzsche disagrees : Long overdue.
  16. #9
  17. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    Arrogance, ichsie, is when you take every brand new ready-made toy and believe that is the "modern" way. But I did not say you are stupid, I have simply said that is a stupid way, which is not the same. I apologize, it was not my intention to offend you.

    It looks like you don't sense that behind those "9 characters" lays a tone of JQuery code you have to load for nothing. Why to use a 2 tone press machine just to break a nut?

    And, yes, my code I use for my personal needs. I don't use frameworks unless I am forced to do that. I am full capable to code anything JQuery can do and more, but exactly in the amount I need to, and I am able to change and modify my codes to fit exactly to my needs. Which you will never be able with JQuery or nomatter other framework.

    And, ChiefWigs1982, I feel very disappointed that you have disagreed which such vehemence my post. I had a long and unselfish contribution to your Forum without any penalty at all so far, thus I don't think I deserved that. Anyway, that will change not an inch of my strong dislike regarding frameworks, you may bet on.

    OK, I admit I was a little bit too harsh with ichsie, but he seems so confident that the javascript world starts and ends with his beloved JQuery

    Comments on this post

    • lnxgeek agrees
    • f_nietzsche agrees : I wholeheartedly support your every post in this thread. It's an oldy, but I thoroughly enjoyed it. I'm happy there is someone else in this forum with a preference for straight JavaScript.
    Last edited by KorRedDevil; January 3rd, 2009 at 08:42 AM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0
    Originally Posted by KorRedDevil
    Arrogance, ichsie, is when you take every brand new ready-made toy and believe that is the "modern" way. But I did not say you are stupid, I have simply said that is a stupid way, which is not the same. I apologize, it was not my intention to offend you.

    It looks like you don't sense that behind those "9 characters" lays a tone of JQuery code you have to load for nothing. Why to use a 2 tone press machine just to break a nut?

    And, yes, my code I use for my personal needs. I don't use frameworks unless I am forced to do that. I am full capable to code anything JQuery can do and more, but exactly in the amount I need to, and I am able to change and modify my codes to fit exactly to my needs. Which you will never be able with JQuery or nomatter other framework.

    And, ChiefWigs1982, I feel very disappointed that you have disagreed which such vehemence my post. I had a long and unselfish contribution to your Forum without any penalty at all so far, thus I don't think I deserved that. Anyway, that will change not an inch of my strong dislike regarding frameworks, you may bet on.

    OK, I admit I was a little bit too harsh with ichsie, but he seems so confident that the javascript world starts and ends with his beloved JQuery
    Everyone's perception of the world starts and ends somewhere, KorRedDevil. I'm a little bit different from you in that I don't automatically assume that my perception of the world is larger and better than others. Everyone has his very own reason and right to believe in his own world.
  20. #11
  21. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    The understanding of the world, even if it is the small javascript world, comes from the basis. Always. Can't you see that javascript is the language itself while a framework is a sort of temporary resume?

    Are you talking about the "modernity"? Now have a look on:
    http://www.ecmascript.org/docs.php

    Now, javascript is about to change... in fact it is changing while we are talking (for instance, as I said, but you have not probably noticed, there is already a getElementsByClassName() method implemented in the modern browsers and I expect the next IE to follow the trend, case in which the solutions presented here as a substitute will become deprecated).

    But the most important change is that javascript could gain a dual class - prototype based skin (so far javascript is just a prototype based OOL).

    Learn more about that on:
    about Object Oriented Languages:
    http://en.wikipedia.org/wiki/Object-...ed_programming
    about prototype based:
    http://en.wikipedia.org/wiki/Prototy...ed_programming
    about class based:
    http://en.wikipedia.org/wiki/Class-based_programming

    As a coder you must know what is changing in the language itself, not to wait your favorite framework to engulf the changes. Javascript has changed a lot within last 10-8-6-4-2 years, and it looks like there will be more changes within the next 2-4-6-8-10 years as well. Before enter in a dispute, make yourself sure you know what you are talking about.

    Now, who's the "modern" oriented guy?
    Last edited by KorRedDevil; January 3rd, 2009 at 12:27 PM.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0
    The understanding of the world, even if it is the small javascript world, comes from the basis. Always. Can't you see that javascript is the language itself while a framework is a sort of temporary resume?
    You haven't read my posts carefully, devil.

    No I can't. Or I might agree with you, it is a temporary resume - but it's so enjoyable to use and saves tons of time from your hard way, average users would still stick to it. It's a new interface that's much easier to use and which you don't believe would change when the *language* itself changes. As if all the framework authors are so stupid that would not change their framework to adapt to the new javascript implementations.

    Are you talking about the "modernity"? Now have a look on:
    http://www.ecmascript.org/docs.php
    Oh, no, devil, relax. I didn't talk about "modernity" that's just a metaphor that I used to compare the jquery way with maybe some of the old ways suggested at the beginning of this thread. It means nothing except the time difference. By modernity I don't mean jquery to be superior than native javascript code but I mean jquery is very good for most of the coders which is justified by its popularity. And you, on the contrary, keep stating how good you are at programming and know stuff and how bad frameworks are and we shouldn't use it - though at middle somewhere you admit that you have been too strong at the beginning because you were our of reasoning for yourself.

    Now, javascript is about to change... in fact it is changing while we are talking (for instance, as I said, but you have not probably noticed, there is already a getElementsByClassName() method implemented in the modern browsers and I expect the next IE to follow the trend, case in which the solutions presented here as a substitute will become deprecated).
    No I do notice things, I just don't show them off at public places to establish my technical superiority over the others. I help people rather than intimidate them.

    But the most important change is that javascript could gain a dual class - prototype based skin (so far javascript is just a prototype based OOL).

    Learn more about that on:
    about Object Oriented Languages:
    http://en.wikipedia.org/wiki/Object...ted_programming
    about prototype based:
    http://en.wikipedia.org/wiki/Protot...sed_programming
    about class based:
    http://en.wikipedia.org/wiki/Class-based_programming
    The fact that you know these mean little here, get back to the thread, devil. Well let me be honest, I previously thought though your points may be wrong but you should be a really good js coder, but are these all that you know? Hey, everyone, this guy knows OOP and he knows that JS is prototype based! What a genius!

    You are starting to talk about everything you know about js and trying to establish your superiority by lecturing people with everything you know. You know the old story of a teacher engaged in a dispute with his student and lost and got all embarrassed by him with red face and blown eyes and had to end the quarral with something like 'I'm your teacher! Look, I know so much more stuff than you do. How could I be wrong?! I'm always right! I'm always right!'.

    Yeah, you are, dude.

    You know what your problem is, devil? You keep underestimating others. You think others should all have the same problems as you have had with existing public frameworks. And you think others don't know a jack about js like you do.

    As a coder you must know what is changing in the language itself, not to wait your favorite framework to engulf the changes. Javascript has changed a lot within last 10-8-6-4-2 years, and it looks like there will be more changes within the next 2-4-6-8-10 years as well. Before enter in a dispute, make yourself sure you know what you are talking about.
    Again please read my post carefully, you still don't get my points.
    A changing javascript, yes, but also changing frameworks. Framework authors are not stupid, I believe they are better js coders than you are - You are the King of your tiny tiny mind world, but they have won the support of millions of coders worldwide. Well I suppose you are smarter than them all.

    Again you are backing me up, thank you. Why should I give so much attention to a changing language? Doesn't it cost more for me to keep up with it because it changes everyday? Why don't I just use other people's work and let others do the keepup work. I don't count on js coding for a living, devil. Nor am I going to encounter any sophisticated problems as you may have met. If I could just make things happen in a few seconds with some framework or library, I'd be happy. I believe I'm speaking for a majority of the js coders there.

    Not to mention those professional ones who are making a live on js coding, they use tons of frameworks and libraries. Again, I guess you are just smarter than them all, aren't you. Can I put it this way, devil: You tried to use a framework and had some bad memories that hurt, thus keeping away as far as you can from them and keep telling your sad stories to us to not use them because you think you are the *smartest* as it's somethnig you failed to deal with, we can't either.


    Now, who's the "modern" oriented guy?
    Yeah, you definitely are, I adore you 100% and you can be a little more childish than you already have been.
  24. #13
  25. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    *sigh* It looks to me like you both overreacted.

    I use jQuery for some paid projects, but prefer to avoid having the extra overhead when I can justify taking the time to create the functionality I need separately.

    Don't forget that many people still have dial-up Internet access.

    Kor, do you have statistics on how much the indexOf() test before the test() test improves performance?
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  26. #14
  27. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    Kravitz, as I said, another coder, rnd me has performed some tests (and I have no reason not to believe him nor to repeat them).
    See the full dialogues about getElementsByClassName() at:
    http://www.codingforums.com/showthread.php?t=154727
    and
    http://www.webdeveloper.com/forum/showthread.php?t=198227

    ichsie
    If you would have known about the ECMAScript intention to propose a new class based face for javascript, you would ask yourself what would become of JQuery and which are the frameworks limits, now and ever. But, on the contrary, it looks like you are very sure that JQuery is the final answer to all the questions, is it?

    I had no intention to establish one's superiority. I have posted those links not for you (frankly said, I don't care whether you know or not what a protototype language is) but for the beginners who might read this thread, as a warning: before deciding to use a framework, they should make sure they do know and understand javascript. That was all. My concern is not you, my friend You look stubborn enough not to care about.

    Another thing: people really have a lot of problems with frameworks. I am a moderator for other 2 coding forums and I see that everyday. And their problems are of two kinds: 1) either they know nothing about javascript and they hope a framework will miraculously save them from learning it or 2) they try to persuade and change the framework code over its limits when trying to customize it.
    but they have won the support of millions of coders worldwide. Well I suppose you are smarter than them all.
    No I am not. Those fanatic supporters are not smart enough, that's the point. By the way, did I mention you that I do co-operate with JQuery crew? They really need a devil's lawyer, and it looks like I do correspond to they expectations

    That means, my friend, that I really have a better perspective about frameworks, from both sides.

    Comments on this post

    • ChiefWigs1982 disagrees : Millions of people are not smart enough becasue they don't share your opinion?! I beg to differ. :/
    Last edited by KorRedDevil; January 3rd, 2009 at 11:50 PM.
  28. #15
  29. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    605
    ChiefWigs1982
    1. I have already said that I was misunderstood as I didn't mean to offend ichsie. I repeat what I have told him in one of my previous posts:
    Originally Posted by KorRedDevil
    "But I did not say you are stupid, I have simply said that is a stupid way, which is not the same. I apologize, it was not my intention to offend you."
    2.
    ChiefWigs1982 disagrees: Millions of people are not smart enough becasue they don't share your opinion?! I beg to differ
    Maybe they don't know all my opinion. Maybe they will share it, if they know it, what do you think?

    It is not my fault that sometime some people are not smart enough. Maybe I am not smart enough as well now and then; it depends on the subject, on the period... People can not be all smart all the time. Nor can be all stupid all the time. What it is certain is that some people are sometimes less smarter. You don't have to be a genius to notice that. Millions of "less smart" Germans do have elected Hitler in 1933. That was not a smart move, what do you think? The fact that millions of people do or think something sometime: now this is not an sustainable argument, is it?

    And it is not about my "opinion" (nor am I alone in this). It is about a principle which I am to defend. Things may not be relative all the time. We need to believe in firm principles as much as we need to doubt them all the time The only way we can bypass this dilemma is to use and analyze arguments. Or I think I have proved some arguments and I expect the opposite side to do the same.

    - Speaking about principles: In my opinion (now this is an opinion, indeed), using frameworks without discrimination and without learning javascript violates at least two principles:

    1. The Occam principle: The way from a cause to a result should pass through less nodes as possible. A framework is a structured library based on a certain language. To use straight the language is the shortest way, not the use of a framework.

    2. The open/closed principle: software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Frameworks extend nothing. They trend to substitute the language itself in practice, which is close to the modification of a language.

    All my life I tried to judge by myself everything and even to doubt my own beliefs if they appeared to be in a logical contradiction. I have never cared about "millions of people", but I have trusted the common sense, which is, by far, another type of paradigm.
    Last edited by KorRedDevil; January 6th, 2009 at 03:07 AM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo