0

I have a jQuery code:

$('.class i').click(function() {
    $(this).parent().find('input').click();
});

Which adds click method to the sibling element when <i> element is clicked.

<div class="class">
    <input>
    <i>
</div>

What would be the same syntax using Angular?

What I have now is:

<div class="class">
    <input>
    <i ng-click="onMyClickFunction($event)">
</div>
onMyClickFunction = function(event){
    angular.element(event.target).parent().find('input').click();
}

I am using Angular's jqLite. But Angular's jqLite has no click() method. This click() method is a native javascript which is not cross-browser supported plus is not supported on mobile devices.

1
  • Maybe you want to use | angular.element('#myselector').trigger('click'); Commented Dec 15, 2015 at 10:36

3 Answers 3

1

Another option would be to not do this using javascript. Just use the default browser behavior, when you click on a label for an input, the input is selected. You could do this with 2 different approaches:

Simply wrap the input and the icon in a label

<label class="class">
    <input>
    <i>
</label>

Or you could use the for attribute

<div class="class">
    <input id="{{ $id }}">
    <label for="{{ $id }}"><i></label>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I guess this is the best solution to the problem. Thank you for thinking this through.
0

Write a directive,

app.directive('clickThis', function () {
    return {
      restrict: 'A',
      link: function(scope, element) {

        element.bind('click', function(e) {
            angular.element(e.target).clickThis('.class').find('input').trigger('click');
        });
      }
    };
});

And your html would be,

<div class="class">
    <input>
    <i clickThis>
</div>

Where clickThis is the Directive.

If you could provide an ID to that input the selection would get simpler. If it is feasible, that is!!

Comments

0

Use trigger('click') instead:

onMyClickFunction = function(event){
    angular.element(event.target).parent().find('input').trigger('click');
}

1 Comment

downvoters never do. Coward. Anyway thanks for your effort. I up-voted you for the change.

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.