2

I am writing a test where I want to click on say the second item in a list and check that something has changed.

I have a bullet list element:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

I have tried the following (element is the angular element containing the entire list):

var li = element.find('li');

li.triggerHandler('click'); //Clicks all elements
li[1].triggerHandler('click'); //Error: li[1].triggerHandler is not a function

None of these works. The first attempt clicks all items and the second throws an error.

Any ideas how to only trigger click on the second item?

4
  • Why aren't you simply binding a click handler to the second element with ng-click? Commented Aug 5, 2015 at 11:30
  • 1
    I am. This is test code where I want to trigger a click on a single element to check that the code in ng-click works as expected. Commented Aug 5, 2015 at 11:42
  • <li ng-click="function()">Item 2</li> Commented Aug 5, 2015 at 11:44
  • Ok I see, I missed that! Commented Aug 5, 2015 at 11:46

1 Answer 1

5

triggerHandler only works on jQuery (or the builtin jqLite) elements. element.find returns a jQuery element (li) but li[1] is a DOM node, not a jQuery wrapped element. You could use element.find('li:eq(1)') (if you use jQuery) or element.find('li').eq(1) (with the builtin jqLite) instead.

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what I was looking for. However, I found out that the correct syntax (in my Jasmine/Karma test) is element.find('li').eq(1). Thanks a lot :)
Right, that's because element.find is "Limited to lookups by tag name" in jqLite. Updated the answer with clarifications for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.