0

I am slowly making my way from jQuery to vanilla JS, but I am struggling with wanting to write the following in JavaScript, would I have to setup a 'for' loop for each element? How would I target the child elements an an ID?

Here's what I would want to understand:

$('#id li').click(function(){
    $(this).addClass('active');
});

I understand document getElementById to grab the '#id' but how would I achieve getting the child <li> elements? And how would I get the 'onclick' function to work for ANY list element clicked? Would I have to setup a 'for' loop for each element? Any help much appreciated!

  • why you need move to thing you named vanilla js (DOM+language standard) ? The fact is not that jquery bad, fact that if you write library yourself there is 70% probability, that your library will work worse than jquery. and yes, you need loop through found elements. Check the jquery sources. to search use [`.querySelector`](https://developer.mozilla.org/en-US/docs/DOM/Document.querySelector) dom function – zb' Feb 02 '13 at 21:40
  • Vanilla JS is raw/plain javascript, whatever you want to call it! –  Feb 02 '13 at 21:45
  • DOM functions are not part of javascript, they part of browser engine implementation. – zb' Feb 02 '13 at 21:47
  • jQuery to JavaScript.......................................... –  Feb 02 '13 at 21:48
  • jQuery is javascript library, just learn javascript and you will find how to use jQuery effective. – zb' Feb 02 '13 at 21:49
  • 2
    That's what I am asking people to help? Did you understand my question? I am learning javascript over jQuery. –  Feb 02 '13 at 21:50
  • i guess that is what Toddo is trying to do. – dan-lee Feb 02 '13 at 21:50
  • yes but learning DOM is timewaste, it constat;y changes,and you can always find currnt state in documentation – zb' Feb 02 '13 at 21:51
  • 1
    HTML and CSS constantly changes too. I don't see your point. –  Feb 02 '13 at 21:52
  • 2
    that is definitely arguable. if you really want to grasp on something, then learning about the internals isn't a waste of time. – dan-lee Feb 02 '13 at 21:53
  • Agree with @DanLee, I think it's integral. –  Feb 02 '13 at 21:53
  • 1
    Yes, learning how something works under the hood is never a waste of time. In this case learning how the DOM works is vital to being a good web dev. jQuery is a great library, but it never hurts to know what is happening under the hood. – DigitalZebra Feb 02 '13 at 22:09
  • @Polaris878 you can always check the source, of jQuery, it is not very hard. – zb' Feb 02 '13 at 22:15
  • I don't think checking the source code of jQuery would be a suitable answer in a job interview to how well someone knows JavaScript. –  Feb 02 '13 at 22:38

3 Answers3

2

Here is a JSFiddle that does what you want:
http://jsfiddle.net/digitalzebra/ZMXGC/10/

(function() {
    var wrapper = document.getElementById("id");
    var clickFunc = function(event) {
        var target = event.originalTarget || event.target;

        target.className = "active";
    };

    wrapper.addEventListener("click",clickFunc);
})();

A little bit of an explanation is in order...

First, I'm using a self executing function to fetch the wrapper div, using getElementById(). This is equivalent to using an id selector in jQuery: $('#id')

Next, I'm attaching a click handler to the element using the function addEventListener() and passing in an event type of click.

This binds the click handler function to the div element, the same as jQuery's click() would do. I'm using something called event bubbling, where a click event on any of the children of the wrapper will call the click handler attached to the wrapper.

Once the user clicks on the element, our function is called. originalTarget or target is the element that the user actually clicked in to, which in this case will always be an li. I'm then setting the class on that element to active.

DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
  • 1
    Change `originalTarget` to `target` – dan-lee Feb 02 '13 at 21:57
  • @Toddo, sorry my example is only working in FireFox right now, give me a sec and I'll get it working in Chrome as well. – DigitalZebra Feb 02 '13 at 21:57
  • You could make a check like `event.target.tagName.toLowerCase() == 'li'` – dan-lee Feb 02 '13 at 21:59
  • @Toddo, I've updated my answer with code that should work in all browsers now. Note that the click handler function is assuming that an `li` was clicked as those are the only children under the `div`. If curious, do some reading about event bubbling/capturing: http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing – DigitalZebra Feb 02 '13 at 22:02
  • Thanks Polaris878, great script, and no for loop too. Is this a cross-browser solution btw? addEventListener etc? I am new to these. –  Feb 02 '13 at 22:28