1

I have the following jQuery code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {


      setTimeout(function() {
        $('.green.bar .inner').css('width', '20%')
      },1000);


    });
</script>

And the html is:

<div class="green bar">
<div class="inner" style="width:10%"></div>
</div>

How can I do what the jQuery code does using just JavaScript?

Thanks a lot

4
  • 5
    That is JavaScript code. Commented Sep 29, 2011 at 13:58
  • Just give in and come to the dark side. Don't make your life more difficult by trying to do something that jQuery already does for you. Commented Sep 29, 2011 at 14:03
  • I think he means "without jQuery" when he says "just JavaScript". And honestly, I use jQuery exactly so I don't have to write this in plain javascript. Commented Sep 29, 2011 at 14:03
  • 4
    Ugh. All these "jQuery is JavaScript" comments that get posted on SO are just pedantic noise. The intent is simple and clear. How to accomplish this without the jQuery abstraction from the native API. Goodness. Commented Sep 29, 2011 at 14:05

1 Answer 1

3

If you mean without using jQuery:

// If you only want to operate on the first match
setTimeout(function(){
    document.querySelector('.green.bar .inner').style.width = '20%';
});

Or:

// If you want to operate on all matches
setTimeout(function(){
    var elements = document.querySelectorAll('.green.bar .inner');
    for(e in elements){
        elements[e].style.width = '20%';
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

To the OP, keep in mind this will work in most browsers, but in the IE world, IE8 and above. Specifically those that support the W3C Selectors API

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.