1

I need to pass an element in ng-click as a param.

<textarea id="chat-msg" name="chat-msg" class="p-r-40 chat-reply form-control"></textarea>
<img src="libs/converse/lib/emoji/smile.png" class="image-size" ng-click="insertEmotion(document.getElementById('chat-msg'))" ></img>

Here onclick works fine, but how can I pass it in ng-click?

3 Answers 3

1

Consider moving the document.getElementById portion into the insertEmotion() function.

Call like this insertEmotion('chat-msg') where insertEmotion takes the element name as its parameter.

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

1 Comment

Yes and I took in controller as document.getElementById(param)
0

It sounds like you may be trying to manipulate the DOM within a controller, which is not really the 'Angular way'. A better approach may be to seek to bind the textarea to a model variable, and have the ngClick directive update the model variable.

For example:

<textarea ng-model="chat"></textarea>
<img src="libs/converse/lib/emoji/smile.png" class="image-size" ng-click="chat = chat + ':-)'" ></img>

Here is a plunkr illustrating this: http://plnkr.co/edit/d0hMVbrwKy6nBgVxWBQP?p=preview

1 Comment

You were right, but when the cusor is in between some words, then still :-) will append at last. So how can I solve this?
0

Maybe try creating chat object and assign the model to the view like this?

http://jsfiddle.net/e9vJH/

var app = angular.module('emotionApp',[]);

app.controller('EmotionCtrl',function($scope) {
    $scope.chat = 'Chat message';
    $scope.insertEmotion = function(emotion) {
        $scope.chat = $scope.chat + emotion;
    };
});


<div ng-app="emotionApp" ng-controller="EmotionCtrl">
    <textarea ng-model="chat"></textarea>
    <hr>
    <button ng-click="insertEmotion(':)')">Insert</button>
</div>

2 Comments

The problem with this is that you dont have access to the element's properties, such as selectionStart, etc.
@mat not sure how that's relevant. The emo would be added when the button is clicked. I do see an issue where I didn't bind the chat to the textarea though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.