1

I have this structure:

<ul>
    <li><a href="mydomain/site/location/paris/">Paris</a></li>
        <ul>
            <li><a href="mydomain/site/location/london/">London</a></li>
        </ul>
    <li><a href="mydomain/site/location/amsterdam-and-paris/">Amsterdam and Paris</a></li>
</ul>

and i need to replace href of each anchor with this:

<a href="mydomain/site/event?location=london">London</a>

Can you help me please

1 Answer 1

10

.attr() has a form when you can use a callback function to transform the currently set attribute.

Using it and combining with the right regex something like this would work:

$("a[href^='mydomain/site']").attr('href', function (i, attr) {
    return attr.replace(
        /^(mydomain\/site\/)(location)\/([^/]*)\//
        , '$1event?$2=$3'
    );
})​
Sign up to request clarification or add additional context in comments.

1 Comment

tnx for the guides, I appreciate it

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.