views:

92

answers:

3

I'm trying to de-couple my dependence on JQuery, as such - I have the following JQuery:

$("#myDIV li").eq(1).html('...');
$("#myDIV li").eq(2).html('...');
$("#myDIV li").eq(3).html('...');

How do I perform the above code without using JQuery (just plain JavaScript).

+8  A: 
var lis = document.getElementById('myIDV').getElementsByTagName('li');
lis[1].innerHTML = '...';
lis[2].innerHTML = '...';
lis[3].innerHTML = '...';

Btw, if you wanna do it with jQuery, save your elements first and work on them, instead of traversing the DOM every time:

var lis = $("#myDIV li");
lis.eq(1).html('...');
lis.eq(2).html('...');
lis.eq(3).html('...');
Andreas Grech
+1  A: 
document.getElementById("myDIV").getElementsByTagName("li")[0].innerHTML = '...';

etc.

However the question remains - why not use jQuery?

Clarification: I'm not trying to suggest that one should ignore how things work. If that's what the OP was going for, then fine. However, I consider jQuery a part of the "standard overhead" for a page nowadays, and don't hesitate to use it even for the tiniest of things, since I'll most likely end up needing more of it later anyways.

Matti Virkkunen
I agree. Why no jQuery?
gurun8
Because for such simple trivial tasks you don't need a framework. Never forget that a library adds another layer of abstraction and the more layers you add, the more problems you can encounter.
Andreas Grech
How about asking *why use jQuery's API in every situation*? If you can use the native API, and there are no compatibility issues, then why suffer the extra overhead? I love jQuery, but more and more am trying to understand and use the underlying API.
patrick dw
As somebody who wandered in the wilderness for a very long time without jQuery, someone who knows how the lower-level APIs work (indeed, how they work *differently* across browsers), I'll pick the framework every single time, and I don't care how simple the page is.
Pointy
@Pointy - I'm sure not leaving jQuery any time soon. But for me it was the opposite. I started off with jQuery, but am more often asking myself *do I really need to turn this element into a jQuery object?*.
patrick dw
I'm wandering the wilderness of no frameworks and am loving it :) I've never used a JavaScript framework except the ones I've written. I prefer the control and understanding of working "low level"...Or at least the DOM's version of "low level"
Bob
I really liked this thread and agree with the OP, I think that understanding what is under the hood (in this case jQuery) also helps you understand jQuery better.
fabio
A: 

that's ok, Bob, but you're slower than those of us who use frameworks. it's a trade off.

Bonk