0

I'm trying to create an if else in jquery with attr (), I want the if else to check if <a href="google.com" class="test" title="test"> exists and if it exists change the href value to another url, for example youtube.com

I got to create an attr () but without if else that worked that model below:

jQuery (document) .ready (function () {
   jQuery ('a.test [title = "test"]'). attr ('href', 'google.com');
})

But I don't want it that way, I want him to take the test first, whether it exists or not.

How can I do this using the jQuery if-else?

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
  • Why would it matter? If you change the href of something that doesn't exist, then nothing would be affected, no errors would be thrown and in general nothing would happen. If you check for existence and decide not to change the href, you'd get the same result - nothing would happen. – VLAZ Apr 06 '20 at 22:33
  • This is just an example, I want to change the href, I want to use the if else to not have an error and it does not replace the url wrong, example it checks if this exists and has the href x, he found this href x and changed to y – Pablo Batista Apr 06 '20 at 22:40
  • Does this answer your question? [Is there an "exists" function for jQuery?](https://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery) – Heretic Monkey Apr 10 '20 at 18:10

0 Answers0