Search code examples
jqueryhtmlhref

Changin href value with jQuery


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.


Solution

  • 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