HandiworkNYC.com
HandiworkNYC.com

Reputation: 11104

jquery attr('href') not working

Update, problem solved. The issue was with a function that I was calling after trying to change the HREF, which was essentially changing it back.


Using jQuery, I am trying to change the HREF attribute of a series of links based on the hash contained in the URL, but the attribute simply is not changing, it's being changed to an empty string.

Can anyone see what I'm doing wrong here? Why isn't it working?

http://fwy.pagodabox.com/categories/sculptures/#grid

The links in question are

All Exhibitions Installations Objects Prints Sculptures

In the secondary nav

function navHash($navlinks, hashtxt) {

        // loop through specified links
        $navlinks.each(function(){
            var $me = $j(this),
                myhref,
                index;

            // Does this link have an href… if not move on          
            if( typeof $me.attr('href') === "undefined" )
                return false;

            myhref = $me.attr('href');
        index = myhref.indexOf('#');

        // if my href doesn't have the specified hash text, add it, else remove it
        if(myhref.indexOf(hashtxt) === -1) {
            $me.attr("href", hashtxt);
        } else {
            $me.attr("href", myhref.substring(0, index));
        }   
    });
}




   $j(document).ready(function($){

        var $navlinks = $j('.sub-nav li:not(.views) a');

        if(window.location.hash == '#grid') {
            navHash($navlinks, '#grid');
            $('.views .ic-grid').click();
        }
// so on...

Thank you!!!

Upvotes: 1

Views: 1917

Answers (1)

Ivan Turovets
Ivan Turovets

Reputation: 164

If you return false here you will stop looping.

if(typeof $me.attr('href') === "undefined")
   return false;

Return true to continue looping.

Upvotes: 1

Related Questions