Beau
Beau

Reputation: 97

Remove string from href

I have this code below that refers to href's that are generally just same-page links like "#programs". However, I now need some of the links to refer to other pages like "/index.html#programs". So my question is, how do I edit the "href" in the code below to be just the number sign and everything after it? Thank you.

    var lastId, topMenu = $("#main-menu"),
topMenuHeight = topMenu.outerHeight() + 500;
menuItems = topMenu.find('a');

    scrollItems = menuItems.map(function () {

        content = $(this).attr("href");

        if(content){
            var checkURL = content.match(/^#([^\/]+)$/i);

            if(checkURL){

                var item = $($(this).attr("href"));
                if (item.length) return item

            }
        }
    });

Upvotes: 0

Views: 368

Answers (3)

charlietfl
charlietfl

Reputation: 171698

If it was me I would forget replacing all the file names and use the hash property of the anchor tag to get what you want

Replace the filter you have with:

.filter(function(){
  return this.hash==='#' +id;
});

Simple demo

Upvotes: 0

Tion
Tion

Reputation: 1490

var item = "/index.html#programs";
var afterHash = item.substr(item.indexOf("#"))

//the trick here is the indexOf and substr methods

var lastId, topMenu = $("#main-menu"),
topMenuHeight = topMenu.outerHeight() + 500;
menuItems = topMenu.find('a');

    scrollItems = menuItems.map(function () {

        content = $(this).attr("href");

        if(content){
            var checkURL = content.match(/^#([^\/]+)$/i);

            if(checkURL){

                var item = $($(this).attr("href"));
                item = item.substr(item.indexOf("#"))
                if (item.length) return item

            }
        }
    });

Upvotes: 0

user2663434
user2663434

Reputation:

u can give new path like this:

var path = "ur_new_location";
$("#link1").attr('href',path);

Upvotes: 1

Related Questions