0

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.

  • possible duplicate of http://stackoverflow.com/questions/5811122/how-to-trigger-a-click-on-a-link-using-jquery – pax162 Nov 08 '13 at 15:36
  • $().click() method binds the click event to an event handler, that is the argument of the click() method. You have specified no handler in your code. Please refer to the jQuery docs here: http://api.jquery.com/click/ – Alberto De Caro Nov 08 '13 at 15:36
  • I was able to see more companies in chrome without any errors. – Ankit Tyagi Nov 08 '13 at 15:37
  • It is working. check the [Fiddle](http://jsfiddle.net/kbJpj/12/) – Murali Murugesan Nov 08 '13 at 15:37
  • Murali, it doesn't on my app. i know that it was suppose to run but when i run it using node js i get this error. Yes i have installed jquery and used it properly. –  Nov 08 '13 at 15:52

6 Answers6

2

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).

Seavel
  • 93
  • 7
0

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.

what is sleep
  • 916
  • 4
  • 12
0

Try:

    $('a:contains("Show more companies")').click(function(){
         var link = $(this).attr('href');
         window.location.href = link;   
    }
Joao Paulo
  • 1,083
  • 4
  • 17
  • 36
0

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

user2932397
  • 184
  • 8
0

Try this:

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

You can also use this...

$(document).ready(function () {
        $('a:contains("Show more companies")').click(function () {
            alert('I am clicked');
        });
    });
Vishal Patel
  • 953
  • 5
  • 11
  • 1
    this is not what he is asking for. he wants to fire the click handler on that page, not create his own handler. – user2932397 Nov 08 '13 at 15:49