2

i've created a function to get the pathname of the webpage and if it matches the id of the menu item then it will add a css property to the div tag, showing the div as current page. heres the website im testing on: http://kwp.host22.com. im using alerts to check that variables are correct. heres my html.

<div id="navigation">
        <a href="index.html"><div class="navblocks" id="index.html"><p>Home</p></div></a>
        <a href="cleaning.html"><div class="navblocks" id="cleaning.html"><p>Cleaning</p></div></a>
        <a href="contact.html"><div class="navblocks" id="contact.html"><p>Contact Us</p></div></a>
    </div>

and heres my jquery:

var path = window.location.pathname;
        if(path === "/")
            {
            var pathname = path.replace("/","index.html");
            }
        else
            {
            pathname = path.replace("/","");
            }
        alert("pathname = " + pathname);
        var id = "#" + pathname;
        alert("id = " + id);
        $('a').each(function() 
            {
            var href = $(this).attr("href");
            alert("href = " + href);
            if (href === pathname)
                {
                $(id).css('box-shadow','0px 0px 20px inset');
                }

but its not applying the box shadow to the div tag.

any help would be appreciated im still learning jquery. thanks

3 Answers 3

3

The issue is that jQuery will interpret the id "#index.html" as "an element with id index and class html". You need to not use a dot in your id for this to work, or escape the dot.

Alternatively, you could do an attribute selector, something like:

$("[href='"+pathname+"']").find('.navblocks').css('box-shadow','0px 0px 20px inset');

Which would be a lot less work overall

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is the . within your id. You need to escape this in order for jQuery to read it as an ID and not an ID followed by a Class selector:

id = "#index\\.html"

For this you can use:

var id = "#" + pathname.replace('.', '\\\.');

If we test this within the JavaScript console on your page, we get the following result:

Working Example

Comments

1

The issue appears to be with the ids that you are using such as #index.html and #cleaning.html to select the correct DOM element. This is incorrect and you should really be just using #index and #cleaning as these are valid IDs (using . in an ID is illegal HTML/XHTML).

One way of getting around this could be to split the id like this so you can then just use the html filename rather than including the file extension:

 var path = window.location.pathname;
        if(path === "/")
            {
            var pathname = path.replace("/","index.html");
            }
        else
            {
            pathname = path.replace("/","");
            }

        pathname = pathname.split(".");
        var id = "#" + pathname[0];

        alert("id = " + id);
        $('a').each(function() 
            {
            var href = $(this).attr("href");
            alert("href = " + href);
            if (href === pathname)
                {
                $(id).css('box-shadow','0px 0px 20px inset');
                }

Now you will be using #index rather than #index.html as the file extension has now been removed. The code that I have added to do this is:

pathname = pathname.split(".");
var id = "#" + pathname[0];

Source: http://www.w3schools.com/jsref/jsref_split.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.