Skip to content Skip to sidebar Skip to footer

Can't Change The Value Of Href With Javascript

This is the code: JS: function f1(){ document.getElementById('test').href='link2'; }; HTML: Text The debugger says

Solution 1:

Where did you put f1? onclick find the function in the global scope, if you didn't define the function in global, then it will not be found.

And $(document).getElementById('test').href="link2"; is wrong too,

it should just be document.getElementById('test').href="link2";

Also, if you are using jQuery, then the best way not use the inline onclick:

$(function(){
    $('#test').click(function() {
        $(this).attr('href', 'link2');
    });
});

Post a Comment for "Can't Change The Value Of Href With Javascript"