João Dias
João Dias

Reputation: 11

How do I test if a HTML link element (a) containing a link href exists at DOM?

I'm trying to check if a link already exists by it href property in a HTML document before to add another one.

The code is just like:

if (!$('a[href*="#businessInfoMenu"]')){
    //add new link   
}

Does anyone made something like that?

Thanks in advance,

Upvotes: 0

Views: 1712

Answers (1)

hunter
hunter

Reputation: 63512

You can check the length of the selector to see if there were any matches:

if ($('a[href*="#businessInfoMenu"]').length == 0) {
    //add new link
}

Upvotes: 3

Related Questions