tags:

views:

68

answers:

1

I have defined styles for <li>

#mainNav{clear:both;background-image: url(../images/bg_menu.jpg);width:975px;height:43px;color:#fff;margin-top:-25px;}
#mainNav ul{list-style: none;padding: 0;margin: 0;}
#mainNav ul li{line-height:43px;float:left;background-image: url(../images/bg_menu.jpg);font-size:15px;font-weight:normal;text-align:center;cursor:default;padding:0px 15px;}
#mainNav ul li:hover{background:#332a03;color:#f7df84;}
<div id="mainNav">
    <ul>
        <li>Home</li>
        <li>Training and Workshops</li>
        <li>Community Consultations</li>
        <li>Community Surveys</li>
        <li>Program and Facility Assesments</li>
        <li>Reports</li>
    </ul>
 </div>

I now want to go back and change the padding for the <li> to '0px 14px' because for some reason it doesn't render right only on Firefox on Mac. So I have this script so far...

var browser = "other";
var OpSys = "other";
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    browser = "Firefox";
}
if (navigator.userAgent.indexOf('Win') != -1) {
    OpSys = "Macintosh";
}
// if its a Mac and Firefox, then change the padding
if (OpSys == "Macintosh" && browser == "Firefox") {
    // how do I change the padding for each <li> ?
}
+2  A: 

Using jQuery:

jQuery("div#mainNav li").each(function() {
   var $li = jQuery(this);
   $li.css("padding", "0px 14px 0px 0px");       
});

Without jQuery:

var liArr = document.getElementsByTagName("LI");

for(var i = 0; i < liArr.length; i++) {
    var parent = liArr[i].parentNode;
    var grandParent = parent.parentNode;

    if(grandParent != null && grandParent.id == "mainNav") {
       liArr[i].style.padding = "0px 14px 0px 0px";
    }        
}
Vivin Paliath
I have multiple sets of lists... I only want to change the navigation list, which is inside <div id="mainNav"> ... </div>
Hristo
jQuery is probably better then because you can use CSS selectors. So instead of *jQuery("li")* do *jQuery("div#mainNav li")*. Otherwise, you need to to check each li element's grandparent to make sure that it is the *mainNav* div. I have edited my solution to show you how to get only the li's that are inside div#mainNav, with jQuery and without.
Vivin Paliath
Just chain it with the "div" first, something like "JQuery("mainNav").("li").each..".
neshaug
its still not working. the code doesn't go inside the if(grandParent != null .... ) statement
Hristo
I'm testing it... and liArr.length returns 0. So the for loop never happens
Hristo
Try a lowercase *li* in the *documents.getElementsByTagName*. If that's not working either, then it's very strange.
Vivin Paliath
lowercase 'li' doesn't work either. it looks like it should work. as i said earlier, liArr.length returns 0. so it seems like the array is not being made properly
Hristo