naijacoder
naijacoder

Reputation: 464

How to change the href for a hyperlink using jQuery:Error href is null

I'm trying to get to the default home link(href) on my sharepoint site using jquery but i keep getting href is undefined. I would like to change it to another URL I have tried

alert($(".static selected menu-item")[0].href).text();



<div class="s4-lp s4-toplinks">
        <div class="s4-tn" id="zz17_TopNavigationMenuV4">
            <div class="menu horizontal menu-horizontal">
                <ul class="root static">
                    <li class="static selected">
                    <a accesskey="1" href="/sites/Home" class="static selected menu-item" style="height: 11px; margin-top: 0px;">
                    <span class="additional-background">
                    <span class="menu-item-text">Home</span>
                    <span class="ms-hidden">Currently selected</span></span></a>
                    </li>

Any ideas what i'm doing wrong Thanks

Upvotes: 0

Views: 1797

Answers (3)

wargodz009
wargodz009

Reputation: 305

change this

alert($(".static selected menu-item")[0].href).text();

to this:

alert($(".static .selected .menu-item").attr('href'));

Upvotes: 1

wsanville
wsanville

Reputation: 37516

Pay close attention to your selector. Class names must start with a dot (.), whereas tag names do not.

Your selector reads:

$(".static selected menu-item")

Which means "a menu-item element that is inside an element of type selected that is inside an element with a class of static.

From your markup, it is unclear what the correct selector is. If you want to select an anchor tag with all three classes, use this:

$(".static.selected.menu-item")

If you want to select an element with the class menu-item inside an element with a class of selected, inside an element with the class static, use this:

$(".static .selected .menu-item")

Upvotes: 1

alex
alex

Reputation: 490433

alert() does not have a text() method.

Try...

$('.static .selected .menu-item').attr('href', 'new_href');

Upvotes: 1

Related Questions