0

Is there a way to use the following code:

$(document).ready(function () {

    $('sd').hover(function() {
      $(this).addClass('a');
    }, function() {
      $(this).removeClass('a');
    });
});

without including the jquery file in my code? For example, is there a way to do exactly the same thing with pure javascript?

Joe Frambach
  • 26,300
  • 10
  • 69
  • 98
Zen 8000k
  • 302
  • 4
  • 11
  • 10
    jQuery is javascript, so yes you can – A. Wolff Jul 11 '13 at 19:58
  • 2
    Please take a look at this [link]http://stackoverflow.com/questions/195951/change-an-elements-css-class-with-javascript – ODelibalta Jul 11 '13 at 19:59
  • 1
    jQuery is _written in_ pure Javascript. You can read its source code. – SLaks Jul 11 '13 at 19:59
  • Yes, but I dopn't want to include a big file in my page when I can avoid doing so... – Zen 8000k Jul 11 '13 at 19:59
  • 3
    Google Document ready without jquery, add remove class JavaScript, mouseover. TADA – epascarello Jul 11 '13 at 20:00
  • It's possible this what you'll need to know: `addEventListener`, `mouseover, mouseout` events and `classList`. – elclanrs Jul 11 '13 at 20:01
  • @elclanrs and probably a thousand checks for compatibility^^ – Christoph Jul 11 '13 at 20:02
  • 1
    `Yes, but I dopn't want to include a big file in my page when I can avoid doing so...` jquery is hardly 100kb..thats not big at all..more then 50% of websites uses jquery... – bipen Jul 11 '13 at 20:03
  • and are you using the minified (.min.js) version – Dany Khalife Jul 11 '13 at 20:04
  • Why waste resources when I can avoid doing so? – Zen 8000k Jul 11 '13 at 20:04
  • 2
    If you're worried about "wasting" the amount of resource it would take up then you need a new host/server... – defaultNINJA Jul 11 '13 at 20:06
  • ok cool...check out.. how long will the above code (5 lines) ends up just using javasript.. i am not against javascript :) and it is much more faster then jquery... but still.. if you are thinking of not using jquery just for resources... then i guess... you are wrong.. and most of the users visitng you site will already have jquery cached in their browser.. – bipen Jul 11 '13 at 20:07
  • 3
    @Zen8000k Because the resource usage difference is negligible in all but the most large-scale environments, and jQuery takes into account lots of compatibility issues, etc you might need to otherwise test for. Also, it's a good bet that the vast majority of people visiting your site already have jQuery in their browser's cache. If you are new enough to JS that you needed to ask this question in the first place, it's incredibly unlikely you are working in an environment where "wasting resources" by including jQuery would have any relevant impact. – Ennui Jul 11 '13 at 20:07
  • 1
    @Ennui exactly, using a CDN you don't waste any ressource (cache/parallel download) – A. Wolff Jul 11 '13 at 20:09
  • But even with CDN I force the user to wait more for the page to load. I understand that, in many cases, programers have to use jQuery, but why make the download time longer when you can do it with an other way? Also, obviously, I am not in the position to edit the JQuery library. – Zen 8000k Jul 11 '13 at 20:12
  • jQuery slows down not just the loading of the page, but the actual operations it perfroms as well, due to it's tall stack of increasingly-unneeded compatability shims. ex: $("title").text("hello world") is 90 function call, document.title="hello world" is 0.... you might consider a compromise like Zepto, i've had good luck using it with bootstrap. – dandavis Jul 11 '13 at 20:14
  • 2
    @dandavis - The OP wants to add a class when the document is ready. How much longer do you expect jQuery to take for that? My guess is most of his users won't mind waiting around for the extra coule nano-seconds. You and the OP are wasting time on unnecessary optimization. – jahroy Jul 11 '13 at 20:18
  • yeah, why use a scalpel when your ignorant client will buy you a full set of cutlery? it's ok to triple the page weight just to add a class, everyone else does it anyway... – dandavis Jul 11 '13 at 20:40

1 Answers1

14

Sure, modified from this answer: Change an element's class with JavaScript

window.onload = function() {
    var elements = document.getElementsByTagName('sd');
    for (var i in elements) {
        if (!elements.hasOwnProperty(i)) continue;
        elements[i].addEventListener( 'mouseover', function() {
            this.className += 'a';
        }
        elements[i].addEventListener( 'mouseout', function() {
            this.className = this.className.replace( /(?:^|\s)a(?!\S)/g , '' );
        }
    }
}
Community
  • 1
  • 1
Joe Frambach
  • 26,300
  • 10
  • 69
  • 98