Why doesn't my $('id').css('property: value'); apply its style?

Go To StackoverFlow.com

0

I'm trying to make a jQuery if statement that says: If a user is on the specified page, then apply specified style; in this case it's putting a bottom-border on an anchor tag's id.

// index.php - referenced html
...
<link href='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' type='text/javascript' />
<style type="text/javascript">
@import url('js/active-links.js');
</style>
...
<ul>
    <li><a id="home" href="#">HOME</a></li>
    <li><a id="other1" href="#">OTHER1</a></li>
    <li><a id="other2" href="#">OTHER2</a></li>
</ul>
...


// active-link.js 
$(document).ready(function() {
    if (document.URL.indexOf('index.php') >= 0) {
        $('#home').css('border-bottom: 18px solid #9cd588');
                                            }
                       });

Ideally, I will have this apply to each link with an else if stating the same thing. Currently it's not doing anything.

2012-04-04 20:36
by beta208
Are you sure your condition evaluates correctly - Madara Uchiha 2012-04-04 20:46
Not entirely, any ideas? It works with another script I had: $(document).ready(function() { if (document.URL.indexOf("contact.php") >= 0) { $('#contact').attr('src','img/m_contact.png'); $('#portfolio').mouseover(function() { .. - beta208 2012-04-04 20:49
You're not doing it right, now I figured out what you're trying to do - Madara Uchiha 2012-04-04 20:51
Shouldn't the below statement work? $(document).ready(function(){ $('#home').css('border-bottom', '18px solid #9cd588'); }) - beta208 2012-04-04 20:56
are you aware that you have multiple severe HTML issues (what's with the type="text/javascript" everywhere?)? you'd probably want to solve these first before addressing problems in the JS or CSS - Eliran Malka 2012-04-05 20:15


0

You're approaching the problem wrong.

In the server side, determine the active link from the menu, and add an active classname to the active menu item.

$active = "Home";
...
echo "<li";
if ($active == "Home") { echo " class=\"active\" "; }
echo ">Home</li>";

Then, in your CSS, without any conditions:

.active { border-bottom: 18px solid #9cd588; }
2012-04-04 20:53
by Madara Uchiha
That is brilliant! Thank you - beta208 2012-04-04 21:12


1

Simple solution:

$('#home').css('border-bottom', '18px solid #9cd588');
2012-04-04 20:38
by mikevoermans
Wrong, the second one will fail. borderBottom - Madara Uchiha 2012-04-04 20:39
I tried both, nothing happened - beta208 2012-04-04 20:41
The top one works. http://jsfiddle.net/GScxM/1/ Are you sure your condition evaluates correctly - Madara Uchiha 2012-04-04 20:45
It definitely works in your fiddle. What am I doing wrong - beta208 2012-04-04 20:55
Ads