How to change CSS settings for all Divs with jQuery

Asked

Viewed 2,012 times

0

I need to change the color of background, the colour of the texts and links of all the Divs of one website. I was able to do this in Javascript with the following code:

<script type="text/javascript">
function altoContraste() {
        var divs = document.getElementsByTagName("div");
        var links = geral.getElementsByTagName("a");


    for (var i = 0; i < divs.length; i++) {
         divs[i].style.background= '#000';
         divs[i].style.color= '#FFF';

    }
    for (var i = 0; i < links.length; i++) {
         links[i].style.color = '#FF0';
    }


}
</script>

But I need to do it using jQuery, I don’t understand jQuery, but I researched some places how to do it and I arrived at the following code:

<script>
$(".altoContrase").click( 
$("div").each(function() {
$(this).css({
'background': '#000', 'color': '#FFF'
})
});
</script>

Why isn’t it working? What should I do to be able to do this? Besides, in this jQuery code I can’t change the color of links.

  • 2

    If you can do with pure javascript what is the idea of doing in jQuery? time wasting. Or have something that is not working?

  • 1

    Is this jQuery code correct? (i.e. it’s the same code you have on your page? it’s missing a parenthesis there, and the way to call the click is passing a function - like you did with the each)

  • The idea of doing in jquery is because javascript is giving problem in changing some things in tables.

  • 1

    jQuery is Javascript. Explaining the problem you have in javascript will be a more important question.

  • I’m still stirring, I couldn’t do anything with jquery. So I should do : $(". altoContrase"). click( Function() { and dps continue as they were ?

  • @Sergio with the javascript code I can’t change the settings of the tables. Making: var tables= general.getelementsbytagname("table"); for (var i = 0; i < tables.length; i++) { tables[i].style.background= '#000'; tables[i].style.color= '#FFF'; } does not work, the table does not change. I also tried to do the same but with TR but tbm it didn’t work

  • @Lucasvilela I think it is better to open a separate question for this, since it has nothing to do with the current question. By the way, I don’t see any problem using jQuery even when you can do it in pure Javascript, because jQuery code is usually more concise. But if you have a solution ready, it’s actually more business to solve its problems than to introduce a heavy framework just for that.

Show 2 more comments

1 answer

2


thus:

<script>
$(document).ready(function(){
   $('.altoContraste').click(function(){
      $('div').css('background-color', '#000');
      $('div').css('color', '#FFF');
      $('a').css('color', '#FF0');
   });
});
</script>

Remember that in your html there must be some button or tag that has the high class

  • It worked, vlw! ?

  • Updated response

  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.