user2911232
user2911232

Reputation:

Click a text link with jquery

I am trying to click the link found at the bottom of this page with the text "Show more companies".

I tried these two ways so far:

    $('a:contains("Show more companies")').click();
    $('a:contains("Show more companies")').trigger('click');

but I am getting this error:

TypeError: Object <a class="button AjaxPagerLink" href="http://www.trustpilot.co.uk/categories/computer?page=2">
Show more companies </a> has no method 'click' at Request._callback (C:\app.js:42:43)

Any clues what the problem is? Is my command correct? Any advice/help is really appreciated.

Edit: Tried all these solutions. Getting the same error.

Upvotes: 0

Views: 1982

Answers (6)

Seavel
Seavel

Reputation: 93

Triggering the class should be easy, but you should verify (it seems so) that there is just an element with that class name.

$(".AjaxPagerLink").trigger("click");

This second version triggers the first class="AjaxPagerLink" element on that page, just to take a wild guess.

$(".AjaxPagerLink")[0].trigger("click");

You can also try a [href^="http"] CSS selector.

$("a[href^='http://www.trustpilot.co.uk/categories/computer?page']").trigger("click");

In general:

Using classes and IDs to retrieve DOM elements is a best practice (as it's also faster than parsing the contained text).

<a href='companies.html' id='show_companies'>Show more companies</a>

You should assign an ID to the element and then trigger a click on that ID:

$('#show_companies').trigger('click');

Just make sure your ID is unique (there must be only a show_companies element in your page).

Upvotes: 2

Vishal Patel
Vishal Patel

Reputation: 973

You can also use this...

$(document).ready(function () {
        $('a:contains("Show more companies")').click(function () {
            alert('I am clicked');
        });
    });

Upvotes: 0

sadkins0101
sadkins0101

Reputation: 71

Try this:

  $('a:contains("Show more companies")')[0].click();

Upvotes: 0

user2932397
user2932397

Reputation: 184

$('a:contains("Show more companies")')[0].click();

Upvotes: 0

Joao Paulo
Joao Paulo

Reputation: 1103

Try:

    $('a:contains("Show more companies")').click(function(){
         var link = $(this).attr('href');
         window.location.href = link;   
    }

Upvotes: 0

what is sleep
what is sleep

Reputation: 905

try this:

$('a.button.AjaxPagerLink').click();

this is better than selecting based on the text.

EDIT: if that is not the issue, then make sure you are calling .click() after you have registered a onClickListener for that link.

Upvotes: 0

Related Questions