bammab
bammab

Reputation: 2563

how to click a link with jquery contains

I have a link that i am trying to emulate a click on. I am using the following code but nothing happens when I execute the script. Any ideas?

Code

$("a:contains('somewebsite')").click();

Link

 <a href="http://somewebsite.com/radio.php">Radio</a>

Upvotes: 1

Views: 2092

Answers (5)

Keithamus
Keithamus

Reputation: 1849

:contains() will check the contents of the text value, which in this case would be "Radio". So this would work:

$("a:contains('Radio')").click();

What you're after, more likely though, is using an attribute contains selector:

$("a[href*='somewebsite']").click();

Which will click a links which an href ([href]) attribute that contains ([href*=""]) somewebsite.

Upvotes: 1

shaunsantacruz
shaunsantacruz

Reputation: 8930

try

$("a[href*='somewebsite']").click();

Upvotes: 0

Marc B
Marc B

Reputation: 360862

You want

$('a[href*="somewebsite"]').click();

instead. As you're using it, it checks for text contents of the tag, not the attributes.

Upvotes: 0

Dennis
Dennis

Reputation: 32608

contains checks for text content. You want an attribute contains selector:

$("a[href*='somewebsite']").click();

Upvotes: 1

Jivings
Jivings

Reputation: 23262

You'd be better off assigning a class or ID to the link. It will be simpler and faster.

<a id="webLink" href="http://somewebsite.com/radio.php">Radio</a>

Script:

$("#webLink").click();

Is there a reason why this method would not be best for you?

Upvotes: 0

Related Questions