I have to use javascript for below mention work which is done in jquery and working perfect. Reason to use javascript is to know how it can be done with javascript.I google it but not find any clue to how get it done
$('.first').find('.sub1').next().css('background','#ff0000')
asked Mar 16, 2013 at 8:29
Jitender
7,99932 gold badges117 silver badges218 bronze badges
-
get jQuery source and work it out, will take some time :)roman m– roman m2013年03月16日 08:32:24 +00:00Commented Mar 16, 2013 at 8:32
2 Answers 2
You won't find a solution for exactly that line of code if that's what you were looking for. If you dig into the DOM you'll eventually figure out what you need, but here's a way, assuming there's only one .sub1 inside each .first.
var els = document.querySelectorAll('.first');
[].forEach.call(els, function(el) {
var next = el.querySelector('.sub1').nextSibling;
next.style.backgroundColor = '#ff0000';
});
answered Mar 16, 2013 at 8:35
elclanrs
94.2k21 gold badges137 silver badges171 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
elclanrs
No, IE9+ only. If you want older browser I'd stick to jQuery instead of the otherwise ugly code you'll have to write...But I'll leave that up to you, you have the link to the docs and some code to work with.
If you don't care about supporting older browsers, this should do the same thing:
[].forEach.call(document.querySelectorAll('.first .sub1 + *'), function(elem) {
elem.style.backgroundColor = '#ff0000';
});
answered Mar 16, 2013 at 8:35
Blender
300k55 gold badges463 silver badges513 bronze badges
Comments
lang-js