JavaScript .css jquery attribute not applying css to div tag jquery apply attribute to multiple elements,jquery apply attribute

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 target='_blank' href="index.html"><div class="navblocks" id="index.html"><p>Home</p></div></a>
        <a target='_blank' href="cleaning.html"><div class="navblocks" id="cleaning.html"><p>Cleaning</p></div></a>
        <a target='_blank' 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

Answer: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

Answer:2

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:

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

Which would be a lot less work overall

Answer:3

var TwitterListModel = function (lists, selectedList) { // [...] this.addUser = function () { if (this.userNameToAdd() && this.userNameToAddIsValid()) { this....

var TwitterListModel = function (lists, selectedList) { // [...] this.addUser = function () { if (this.userNameToAdd() && this.userNameToAddIsValid()) { this....

  1. what is bond used for
  2. bind used as a noun in a sentence
  3. bind used as a noun

I would like to know if there is a way to modify an html item (i.e. a div) when a radio button is selected without using js. Here is the markup: <div class="container"> <input id="...

I would like to know if there is a way to modify an html item (i.e. a div) when a radio button is selected without using js. Here is the markup: <div class="container"> <input id="...

  1. highlighting html element

I was trying the following: f.addEventListener('submit',(function(frm){ var func = (function(e){somefunction(e,frm);})(e); })(f),false); But this is failing. I want to pass the form (f) as a static ...

I was trying the following: f.addEventListener('submit',(function(frm){ var func = (function(e){somefunction(e,frm);})(e); })(f),false); But this is failing. I want to pass the form (f) as a static ...

  1. javascript passing event as parameter
  2. javascript passing event to function
  3. javascript passing event
  4. javascript passing event onclick
  5. javascript onclick event passing parameters
  6. javascript passing parameters to event handlers
  7. javascript passing this to event handler
  8. javascript passing arguments to event handlers

so, I came across a bug and thought it was interesting. Once I sat and thought about it for 5 seconds, it made sense but curious how one would get past it in the future. so, I have some hashes set up ...

so, I came across a bug and thought it was interesting. Once I sat and thought about it for 5 seconds, it made sense but curious how one would get past it in the future. so, I have some hashes set up ...