1

I have the following test html:

<div class="resultsFooter">
    <ul id="ResultFooter">
        <li id="Preferences">
            <a title="Preferences" id="PreferenceLink" href="http://google.com">Preferences</a>
        </li>
        <li id="Advanced">
            <a title="Advanced" id="AdvancedLink" href="http://ask.com">Advanced</a>
        </li>
    </ul>
</div>

I am trying to change the href value of the second a link from "http://ask.com" to "http://bing.com" using jquery. I have the below jquery but it does not seem to do trick, I am missing something in the selector:

$(document).ready(function() {

$('a#AdvancedLink').attr('href', 'http://www.bing.com');

});

Any suggestion on this would be helpful.

Thanks in advance.

Dev P
  • 1,157
  • 5
  • 32
  • 54

2 Answers2

2

Your original code works fine, however you can remove the a from the selector since the ID should be unique:

$(document).ready(function() {
     $('#AdvancedLink').attr('href', 'http://www.bing.com');
});

http://jsfiddle.net/S2NkH/

If you inspect the markup in the JSFiddle, you can see that Advanced has the href value set to bing.com

Darren
  • 68,902
  • 24
  • 138
  • 144
  • What if the same JavaScript file was included on two pages with `#AdvancedLink` elements, but only one of them was an `a` element? :) (Wasn't me who downvoted, in case you're wondering). – James Donnelly Feb 25 '14 at 13:59
  • @JamesDonnelly - I would question the developer who named the element `AdvancedLink` if it was not referring to anchor/`hyperlink` element. – Darren Feb 25 '14 at 14:01
0

its being an id(unique) you can directly refer it only by that id

$(document).ready(function() {

$('#AdvancedLink').attr('href', 'http://www.bing.com');

});
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53