How do I change the url using jQuery?

Let's assume it's in a.html.

How to change url to b.html using jQuery?

+2


source to share


3 answers


I am assuming you want to change the target ( href

) of an element <a>

using jQuery?

$("a#your_id").attr("href", "b.html"); // where `a#your_id` is your selector

      



Or do you want to change the current location in the browser (redirect someone to another page)?

window.location = "b.html"; // No jQuery required for this!

      

+7


source


if your question is how to change the existing href = a.html to href = b.html then this should work.



$('a[href=a.html]').attr('href', 'b.html');

      

+1


source


If you want to assign b.html to your tag, you need to use the 'src' attribute.

$('a#your_id').attr('src', 'b.html');

      

0


source







All Articles