0

In the HTML source code of a page, we have:

<li class="wv-topbar__nav__item">
  <a href="/go/business_savings/933dd921-b3d4-be4d-bb0d-e45fe696643d/" class="wv-topbar__nav__link">
    Save Money
  </a>
</li>

I want to be able to get this string from the href value:

933dd921-b3d4-be4d-bb0d-e45fe696643d

Basically, the string between the 3rd and 4th slash and store it into a variable.

0

3 Answers 3

2

Just one liner :

  alert($('.wv-topbar__nav__item a') // select anchor element inside the element with class = wv-topbar__nav__item
   .attr('href').split('/')[3]);

Example : https://jsfiddle.net/DinoMyte/cfrjqvya/

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

Comments

2

In jQuery, it would look something like this

var urlKey = $(".wv-topbar__nav__item a").prop("href").split("/")[3]

To access the href attribute, we can use either the attr or prop functions. This returns us a string.

To find the part after a delimiter, we can call the split method and take the fourth result.

Comments

1
var result = $('.wv-topbar__nav__link').attr('href').split('/')[3];

4 Comments

that won't work since li doesn't have href attribute
yep add the descendant selector after your selector and it will work
I am using the class of the anchor, not the li. This would be true for ".wv-topbar__nav__item"
no worries! I would argue this is better than adding the anchor selector after the li's class since it may break in the future if more anchors are added to the li

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.