4

How can a css style subclass can be changed with javaScript ?

To make myself clear let's say we have the following code:

ul#main-menu{
margin:0 0 0 285px;
padding:0 0 0 0;
list-style:none;    
}

ul#main-menu li a{
margin:0 0 0 0;
padding:8px 10px 7px 10px;
color:#FFF;
}

I can change the margin of the ul#main-menu using the code: document.getElementById('main-menu').style.marginLeft='10px';

So how can one change the color of li a using javaScript ?

Suciu Lucian
  • 430
  • 4
  • 12

4 Answers4

2

Using the Selectors API

var anchors = document.querySelectorAll('ul#main-menu li a');

for(var i = 0, len = anchors.length; i < len; i ++) {
    anchors[i].style.color = 'red';
}

Using plain old JavaScript:

var lis = document.getElementById('main-menu').children;

Array.prototype.forEach.call(lis, function(li) {
    var anchors = li.getElementsByTagName('a');
    Array.prototype.forEach.call(anchors, function(a) {
        a.style.color = 'green';
    });
});

jsFiddle Demo

c.P.u1
  • 16,664
  • 6
  • 46
  • 41
1

Without using jQuery, this snippet will store each a it can find in each li in your main menu. Then, it will go through each item and set its text color to red.

var a_list = document.querySelectorAll('#main-menu li a');

for (var i=0; i<a_list.length; i++) {
    a_list[i].style.color = 'red';
}

If you're using jQuery, you can accomplish the same thing like so:

$('#main-menu li a').css('color','red');

However, be aware that it is not good practice to set style rules with JavaScript, as this is what CSS was designed for. It would be much better if you used JavaScript to add a class (perhaps something like .higlighted-text) to your a elements that therefore behave like you wish.

Bryce
  • 6,440
  • 8
  • 40
  • 64
  • Ok, so let's say now there is a hover effect like this ul#main-menu li a:hover , using your code the hover dosen't work anymore. How can the hover color can be changed ? – Suciu Lucian Aug 30 '13 at 04:57
  • It's not working because the JavaScript is placing the color override on the element itself, rather than changing the CSS class in your `.css` file. If you want this to work, you either need to use JS to change the color on hover, or use JS to add a *class that defines your color style* which *can* be overridden by the `:hover` style. – Bryce Aug 30 '13 at 05:02
  • 1
    something like `$('#main-menu li a').hover(function() { $(this).toggleClass('red-text'); });` – Bryce Aug 30 '13 at 05:04
  • Oh, I see.. I was just reading on changing the hover css proprieties with JS, and everyone is saying it`s not possible only by replacing the class. How can the hover css style can be changed with JS ? btw I am looking for a non jQuery solution to this. – Suciu Lucian Aug 30 '13 at 05:08
  • Without jQuery, you really don't want to go down that road. see: http://stackoverflow.com/questions/6130737/mouseenter-without-jquery ...It will be much better if you're using native CSS abilities, and simply adding/removing classes when you need. – Bryce Aug 30 '13 at 05:14
0
document.getElementById('main-menu').getElements("li a").style.color="some_color";
// Or simply
document.getElements("#main-menu li a").style.color="some_color";
Cole Pilegard
  • 582
  • 2
  • 11
0
$('#main-menu li a').css('color':'Red');
S. S. Rawat
  • 5,943
  • 4
  • 43
  • 59