Results 1 to 7 of 7

Thread: Stop element from losing hover colour

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default Stop element from losing hover colour

    Hey, I'm trying to change the colour of an element in an unordered list with javascript but when I do this the hover colour no longer shows.

    Here's the HTML:

    Code:
    <ul id="nav">
    <li><a class="selected" id="first" href="index.php" target="_self">Home</a></li>
    <li><a href="venueinfo.html" target="_self">Venue info</a></li>
    <li class="dotted"><a href="links.html" target="_self">Links</a></li>
    <li><a id="monthall" class="selected" onclick="showMonth('All')" target="_self">All</a></li>
    <li><a id="month5" onclick="showMonth(5)" target="_self">May</a></li>
    <li><a id="month6" onclick="showMonth(6)" target="_self">June</a></li>
    <li><a id="month12" onclick="showMonth(12)" target="_self">December</a></li>
    </ul>
    Hover CSS and "selected" class:

    Code:
    #sidebar ul#nav a.selected{
    background-color:#66cc33;
    color:#003300;
    }
    
    #nav a:hover{
    background-color: #66cc33;
    color:#003300;
    }
    The following JavaScript makes a call to a PHP file which returns content for that specific month so that's why the colour changes to show what month the user has clicked on:

    Code:
    function showMonth(str)
    { 
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      } 
    var url="sorting.php";
    
    var i = 1;
    
    for(i = 1; i <= 12; i++) {
    	try { 
    		document.getElementById('month' + i).style.backgroundColor = '#003300';
    		document.getElementById('month' + i).style.color = '#66cc33';
    	}
    		
    	catch(err) {
    			
    	}
    }
    
    if(str == 'All') {
    	document.getElementById('monthall').style.backgroundColor = '#66cc33';
    	document.getElementById('monthall').style.color = '#003300';
    }
    
    else {
    document.getElementById('month' + str).style.backgroundColor = '#66cc33';
    document.getElementById('month' + str).style.color = '#003300';
    document.getElementById('monthall').style.backgroundColor = '#003300';
    document.getElementById('monthall').style.color = '#66cc33';
    }
    
    document.getElementById('ajaxload').style.display='block';
    document.getElementById('events').style.display='none';
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    
    }
    My JavaScript skillz aren't exactly great so the code may look very inefficient, but primarily I just want the script to not interfere with the "hover" class in the CSS.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    The problem is not in the Javascript, but a missing rule on your CSS.

    See if adding highlighted helps:
    Code:
    #nav li a:hover{
    background-color: #66cc33 !important;
    color:#003300 !important;
    }
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    Schmoopy (05-20-2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Thanks for that but it doesn't fix the problem =/

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Please post a link to the page on your site that contains the problematic script so we can check it out.
    Jeremy | jfein.net

  6. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Oki, I put it up here:

    http://bristoldnb.co.uk/testing/

    If you roll over the links on the side where the months are, under "All" you'll see the colours change but then after you click on one of the months it stops.

    Hope this helps.

  7. #6
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Excuse me if I'm being somewhat simple here, but if you're trying to indicate which month they selected couldn't you just use the 'visited' property for the<a> tag.

    ie.
    HTML Code:
    #nav li a:visited{
    background-color: #66cc33 !important;
    color:#003300 !important;
    }
    Then you don't need the javascript to change the colour.

    You may need a script to unset the month tag from 'visited' when they return to the main page.

  8. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ah, fixed it now, I changed the wrong part in the CSS with the important tag.

    I can't use the visited propery because it's done with AJAX and the page remains the same.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •