Search code examples
javascriptjqueryhtmlhref

Change href in HTML tag does not works


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.


Solution

  • 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).