Change href in HTML tag does not works












0















I have this HTML tag:



<a href="show_comments.php?id=6">(show comments)</a>


I would like to change its href. I am triying with jquery but It does not works:



$('a[href="show_comments.php?id=6"]').attr('href', 'xyz.php');


I also tried with querySelectorAll:



document.querySelectorAll("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php');


UPDATE:



It was a timing problem. Using $( document ).ready(function() {}); both solutions works.



Why these solutions don't work? I would like to do it without jQuery but any solution is appreciated.










share|improve this question

























  • What exactly isn't working here?

    – ksav
    Nov 24 '18 at 15:14











  • I've got the solution. Main problem was that the script tag executed before the page was loaded. :)

    – Miguel.G
    Nov 24 '18 at 15:16
















0















I have this HTML tag:



<a href="show_comments.php?id=6">(show comments)</a>


I would like to change its href. I am triying with jquery but It does not works:



$('a[href="show_comments.php?id=6"]').attr('href', 'xyz.php');


I also tried with querySelectorAll:



document.querySelectorAll("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php');


UPDATE:



It was a timing problem. Using $( document ).ready(function() {}); both solutions works.



Why these solutions don't work? I would like to do it without jQuery but any solution is appreciated.










share|improve this question

























  • What exactly isn't working here?

    – ksav
    Nov 24 '18 at 15:14











  • I've got the solution. Main problem was that the script tag executed before the page was loaded. :)

    – Miguel.G
    Nov 24 '18 at 15:16














0












0








0


1






I have this HTML tag:



<a href="show_comments.php?id=6">(show comments)</a>


I would like to change its href. I am triying with jquery but It does not works:



$('a[href="show_comments.php?id=6"]').attr('href', 'xyz.php');


I also tried with querySelectorAll:



document.querySelectorAll("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php');


UPDATE:



It was a timing problem. Using $( document ).ready(function() {}); both solutions works.



Why these solutions don't work? I would like to do it without jQuery but any solution is appreciated.










share|improve this question
















I have this HTML tag:



<a href="show_comments.php?id=6">(show comments)</a>


I would like to change its href. I am triying with jquery but It does not works:



$('a[href="show_comments.php?id=6"]').attr('href', 'xyz.php');


I also tried with querySelectorAll:



document.querySelectorAll("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php');


UPDATE:



It was a timing problem. Using $( document ).ready(function() {}); both solutions works.



Why these solutions don't work? I would like to do it without jQuery but any solution is appreciated.







javascript jquery html href






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 15:17







Miguel.G

















asked Nov 24 '18 at 15:10









Miguel.GMiguel.G

9810




9810













  • What exactly isn't working here?

    – ksav
    Nov 24 '18 at 15:14











  • I've got the solution. Main problem was that the script tag executed before the page was loaded. :)

    – Miguel.G
    Nov 24 '18 at 15:16



















  • What exactly isn't working here?

    – ksav
    Nov 24 '18 at 15:14











  • I've got the solution. Main problem was that the script tag executed before the page was loaded. :)

    – Miguel.G
    Nov 24 '18 at 15:16

















What exactly isn't working here?

– ksav
Nov 24 '18 at 15:14





What exactly isn't working here?

– ksav
Nov 24 '18 at 15:14













I've got the solution. Main problem was that the script tag executed before the page was loaded. :)

– Miguel.G
Nov 24 '18 at 15:16





I've got the solution. Main problem was that the script tag executed before the page was loaded. :)

– Miguel.G
Nov 24 '18 at 15:16












3 Answers
3






active

oldest

votes


















1














Try adding an id to this element and then selecting this id in jQuery or by play JS



Let's assume that the tag has an id called changeUrl



$("#changeUrl").attr('href', 'xyz.php');


or



document.querySelector("#changeUrl").setAttrtibute('href','xyz.php');





share|improve this answer


























  • .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

    – Christian Juth
    Nov 24 '18 at 15:25






  • 1





    I edited it thanks for the note

    – Coding Nerd
    Nov 24 '18 at 15:30





















0














You'll have to do it individually on every node when you're using querySelectorAll because it returns you a NodeList which doesn't have setAttribute method. Only nodes have that method:



document.querySelectorAll("a[href='show_comments.php?id=6']").forEach(node => node.setAttrtibute('href','xyz.php'))


EDIT: If you have just a single node, use document.querySelector



document.querySelector("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php')


Load your JS just before </body> to ensure JS has access to complete DOM






share|improve this answer





















  • 3





    That's not true of the jQuery case though

    – charlietfl
    Nov 24 '18 at 15:14











  • Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

    – Miguel.G
    Nov 24 '18 at 15:15











  • @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

    – mehulmpt
    Nov 24 '18 at 15:17



















0














SOLVED:
It was a timing problem. Using $( document ).ready(function() {}); both solutions work.



The issue was that it is a CSRF attack, so html have to be loaded to attack successfully (the tag I wanted to change was loaded after the JS script).






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459505%2fchange-href-in-html-tag-does-not-works%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Try adding an id to this element and then selecting this id in jQuery or by play JS



    Let's assume that the tag has an id called changeUrl



    $("#changeUrl").attr('href', 'xyz.php');


    or



    document.querySelector("#changeUrl").setAttrtibute('href','xyz.php');





    share|improve this answer


























    • .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

      – Christian Juth
      Nov 24 '18 at 15:25






    • 1





      I edited it thanks for the note

      – Coding Nerd
      Nov 24 '18 at 15:30


















    1














    Try adding an id to this element and then selecting this id in jQuery or by play JS



    Let's assume that the tag has an id called changeUrl



    $("#changeUrl").attr('href', 'xyz.php');


    or



    document.querySelector("#changeUrl").setAttrtibute('href','xyz.php');





    share|improve this answer


























    • .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

      – Christian Juth
      Nov 24 '18 at 15:25






    • 1





      I edited it thanks for the note

      – Coding Nerd
      Nov 24 '18 at 15:30
















    1












    1








    1







    Try adding an id to this element and then selecting this id in jQuery or by play JS



    Let's assume that the tag has an id called changeUrl



    $("#changeUrl").attr('href', 'xyz.php');


    or



    document.querySelector("#changeUrl").setAttrtibute('href','xyz.php');





    share|improve this answer















    Try adding an id to this element and then selecting this id in jQuery or by play JS



    Let's assume that the tag has an id called changeUrl



    $("#changeUrl").attr('href', 'xyz.php');


    or



    document.querySelector("#changeUrl").setAttrtibute('href','xyz.php');






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 24 '18 at 15:29

























    answered Nov 24 '18 at 15:17









    Coding NerdCoding Nerd

    468




    468













    • .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

      – Christian Juth
      Nov 24 '18 at 15:25






    • 1





      I edited it thanks for the note

      – Coding Nerd
      Nov 24 '18 at 15:30





















    • .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

      – Christian Juth
      Nov 24 '18 at 15:25






    • 1





      I edited it thanks for the note

      – Coding Nerd
      Nov 24 '18 at 15:30



















    .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

    – Christian Juth
    Nov 24 '18 at 15:25





    .changeUrl is a class, not an id. You can make it an id if you want, but your example code is not using an id. Also, it would be helpful to provide the updated HTML code with added the id/class.

    – Christian Juth
    Nov 24 '18 at 15:25




    1




    1





    I edited it thanks for the note

    – Coding Nerd
    Nov 24 '18 at 15:30







    I edited it thanks for the note

    – Coding Nerd
    Nov 24 '18 at 15:30















    0














    You'll have to do it individually on every node when you're using querySelectorAll because it returns you a NodeList which doesn't have setAttribute method. Only nodes have that method:



    document.querySelectorAll("a[href='show_comments.php?id=6']").forEach(node => node.setAttrtibute('href','xyz.php'))


    EDIT: If you have just a single node, use document.querySelector



    document.querySelector("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php')


    Load your JS just before </body> to ensure JS has access to complete DOM






    share|improve this answer





















    • 3





      That's not true of the jQuery case though

      – charlietfl
      Nov 24 '18 at 15:14











    • Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

      – Miguel.G
      Nov 24 '18 at 15:15











    • @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

      – mehulmpt
      Nov 24 '18 at 15:17
















    0














    You'll have to do it individually on every node when you're using querySelectorAll because it returns you a NodeList which doesn't have setAttribute method. Only nodes have that method:



    document.querySelectorAll("a[href='show_comments.php?id=6']").forEach(node => node.setAttrtibute('href','xyz.php'))


    EDIT: If you have just a single node, use document.querySelector



    document.querySelector("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php')


    Load your JS just before </body> to ensure JS has access to complete DOM






    share|improve this answer





















    • 3





      That's not true of the jQuery case though

      – charlietfl
      Nov 24 '18 at 15:14











    • Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

      – Miguel.G
      Nov 24 '18 at 15:15











    • @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

      – mehulmpt
      Nov 24 '18 at 15:17














    0












    0








    0







    You'll have to do it individually on every node when you're using querySelectorAll because it returns you a NodeList which doesn't have setAttribute method. Only nodes have that method:



    document.querySelectorAll("a[href='show_comments.php?id=6']").forEach(node => node.setAttrtibute('href','xyz.php'))


    EDIT: If you have just a single node, use document.querySelector



    document.querySelector("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php')


    Load your JS just before </body> to ensure JS has access to complete DOM






    share|improve this answer















    You'll have to do it individually on every node when you're using querySelectorAll because it returns you a NodeList which doesn't have setAttribute method. Only nodes have that method:



    document.querySelectorAll("a[href='show_comments.php?id=6']").forEach(node => node.setAttrtibute('href','xyz.php'))


    EDIT: If you have just a single node, use document.querySelector



    document.querySelector("a[href='show_comments.php?id=6']").setAttrtibute('href','xyz.php')


    Load your JS just before </body> to ensure JS has access to complete DOM







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 24 '18 at 15:15

























    answered Nov 24 '18 at 15:13









    mehulmptmehulmpt

    9,163103063




    9,163103063








    • 3





      That's not true of the jQuery case though

      – charlietfl
      Nov 24 '18 at 15:14











    • Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

      – Miguel.G
      Nov 24 '18 at 15:15











    • @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

      – mehulmpt
      Nov 24 '18 at 15:17














    • 3





      That's not true of the jQuery case though

      – charlietfl
      Nov 24 '18 at 15:14











    • Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

      – Miguel.G
      Nov 24 '18 at 15:15











    • @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

      – mehulmpt
      Nov 24 '18 at 15:17








    3




    3





    That's not true of the jQuery case though

    – charlietfl
    Nov 24 '18 at 15:14





    That's not true of the jQuery case though

    – charlietfl
    Nov 24 '18 at 15:14













    Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

    – Miguel.G
    Nov 24 '18 at 15:15





    Thanks you very much!! It helps a lot. I also use $( document ).ready(function()}); to execute it after page loads. Thanks:)

    – Miguel.G
    Nov 24 '18 at 15:15













    @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

    – mehulmpt
    Nov 24 '18 at 15:17





    @charlietfl yeah I missed that detail. But the OP answered that he was loading script before, so its fine now

    – mehulmpt
    Nov 24 '18 at 15:17











    0














    SOLVED:
    It was a timing problem. Using $( document ).ready(function() {}); both solutions work.



    The issue was that it is a CSRF attack, so html have to be loaded to attack successfully (the tag I wanted to change was loaded after the JS script).






    share|improve this answer






























      0














      SOLVED:
      It was a timing problem. Using $( document ).ready(function() {}); both solutions work.



      The issue was that it is a CSRF attack, so html have to be loaded to attack successfully (the tag I wanted to change was loaded after the JS script).






      share|improve this answer




























        0












        0








        0







        SOLVED:
        It was a timing problem. Using $( document ).ready(function() {}); both solutions work.



        The issue was that it is a CSRF attack, so html have to be loaded to attack successfully (the tag I wanted to change was loaded after the JS script).






        share|improve this answer















        SOLVED:
        It was a timing problem. Using $( document ).ready(function() {}); both solutions work.



        The issue was that it is a CSRF attack, so html have to be loaded to attack successfully (the tag I wanted to change was loaded after the JS script).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 21:06









        cezar

        5,71732455




        5,71732455










        answered Nov 24 '18 at 15:20









        Miguel.GMiguel.G

        9810




        9810






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459505%2fchange-href-in-html-tag-does-not-works%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python