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.
javascript jquery html href
add a comment |
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
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
add a comment |
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
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
javascript jquery html href
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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');
.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
add a comment |
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
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
add a comment |
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).
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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');
.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
add a comment |
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');
.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
add a comment |
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');
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');
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
add a comment |
.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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited Nov 24 '18 at 21:06
cezar
5,71732455
5,71732455
answered Nov 24 '18 at 15:20
Miguel.GMiguel.G
9810
9810
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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