I thought I got this one right, apparently not as I’m getting an “undefined” error
Here is my CSS
JavaScript
x
9
1
<main id="tm-content" class="tm-content">
2
<ul class="uk-breadcrumb">
3
<li>
4
<a href="/countries">Countries</a>
5
</li>
6
<li>
7
<a href="/countries/united-states">United States</a>
8
</li>
9
Will this bit of javascript define https://www.example.com/countries
?
JavaScript
1
4
1
function getHref() {
2
return $('.uk-breadcrumb li a')[0].href
3
}
4
Any suggestions or guidance would be very much appreciated
Advertisement
Answer
It looks like you might not have the $
defined.
For this simple case, you should be able to use document.querySelectorAll
in its place:
JavaScript
1
5
1
function getHref() {
2
return document.querySelectorAll('.uk-breadcrumb li a')[0].href;
3
}
4
5
alert(getHref());
JavaScript
1
7
1
<ul class="uk-breadcrumb">
2
<li>
3
<a href="/countries">Countries</a>
4
</li>
5
<li>
6
<a href="/countries/united-states">United States</a>
7
</li>
Note that this doesn’t have full support below IE9, if you need that then i’d recommend jQuery.