Jitender
Jitender

Reputation: 7969

Convert jquery function into javascript

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

http://jsfiddle.net/2vP24/

$('.first').find('.sub1').next().css('background','#ff0000')

Upvotes: 0

Views: 101

Answers (2)

Blender
Blender

Reputation: 298582

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';
});

Upvotes: 1

elclanrs
elclanrs

Reputation: 94141

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';
});

Upvotes: 2

Related Questions