0

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')
1
  • get jQuery source and work it out, will take some time :) Commented Mar 16, 2013 at 8:32

2 Answers 2

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';
});
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.