Creek Barbara
Creek Barbara

Reputation: 647

Jquery: Focus last line on textarea on change

I'm trying to focus the last line of a textarea with the id "textarealog" on textarea change. I don't get what is wrong in the script below. Your help will be appreciated.

HTML

<textarea class="textarea textarealog textarealogtextarea" id="textarealog" variable="#a"></textarea>

Javascript

jQuery.fn.lockCursor = function() {
    return this.each(function() { 
        if (this.setSelectionRange) { 
            var len = $(this).val().length * 2; 
            this.setSelectionRange(len, len);
        } else {
            $(this).val($(this).val()); 
       }
    });
};

$('.textarealog').on('change', function(){
    $(this).lockCursor();
});

JSFiddle here : https://jsfiddle.net/vfbhd2r9/

Upvotes: 2

Views: 807

Answers (2)

Creek Barbara
Creek Barbara

Reputation: 647

I solved my issue with a bind function:

$('#textarealog').bind('input propertychange', function(){
    $(this).lockCursor();
});

Upvotes: 1

Akhtar Munir
Akhtar Munir

Reputation: 1769

If you are using Id then you should use #, because you are passing Id and below you are getting a class. Use this might help you

      $("#textarealog")

Upvotes: 1

Related Questions