June 18, 2013

why i cant empty textarea when i enter

HiDd3N’s Question:

i just setup a jsfiddle in this link and the problem is where i empty textarea one line break stay and i cant see placeholder again and i always one break line is there see this fiddle

http://jsfiddle.net/W5WE8/

my html

<script src="http//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<textarea id="mytextarea" rows="15" cols="80" placeholder="write something and press enter"></textarea>

and here is my js

$('#mytextarea').keydown(function(e) {
        if (e.keyCode == 13) {
            $('#mytextarea').val('').focus();
        }
    });

You need to prevent the default behaviour of the keydown. As seen here: http://jsfiddle.net/lnrb0b/W5WE8/2/

$('#mytextarea').keydown(function (e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        $('#mytextarea').val('').focus();
    }
});

Hope that helps 🙂

The main reason why this is happening is because of the enter key. This will add one break line before doing what you need.

Prevent the event propagation as suggested in other answers.

April 14, 2012

How to set cursor at the end in a TEXTAREA? (by not using jQuery)

Question by Brian Hawk

Is there a way to set the cursor at the end in a TEXTAREA tag? I’m using Firefox 3.6 and I don’t need it to work in IE or Chrome. JavaScript is ok but it seems all the related answers in here use onfocus() event, which seems to be useless because when user clicks on anywhere within textarea, Firefox sets cursor position to there. I have a long text to display in a textarea so that it displays the last portion (making it easier to add something at the end).

Answer by Starx

Here is a function for that

function moveCaretToEnd(el) {
    if (typeof el.selectionStart == "number") {
        el.selectionStart = el.selectionEnd = el.value.length;
    } else if (typeof el.createTextRange != "undefined") {
        el.focus();
        var range = el.createTextRange();
        range.collapse(false);
        range.select();
    }
}

[Demo][Source]

April 9, 2012

Cross-browser method to prevent all methods of text copying from a textarea?

Question by think123

I am working on an online typing software. In the typing software, all is going well but I have the problem of dishonest users who might possibly type the text into the textarea, copy it, then reload the page (therefore resetting the timer) and pasting it in straightaway. So I was thinking along the lines of using something like evt.preventDefault(); when javascript detects the pressing of the ctrl / cmd button along with the C key. But then I realized that the user could always go up to the menu bar to press Edit -> Copy. So I was wondering, is there a cross-browser method to disable both methods of copying?

Answer by Selcuk

You can try to use the following jQuery code:

$('input[type=text],textarea').bind('copy paste cut drag drop', function (e) {
   e.preventDefault();
});

Answer by Starx

Bind the event handlers and prevent the clipboard function like such:

$('textarea').on('copy paste cut drag drop', function (e) {
   e.preventDefault();
});
March 9, 2012

Textarea not showing hebrew text from mysql

Question by Prateek

I’m trying to return some particular text from mysql into a textarea. Now this text returning from mysql has

    collation=utf8_unicode_ci 

and the charset is set by adding this line in my php file.

   <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-8-i" />

Cant figure out the problem here. Everything looks alright but why is the textarea not showing hebrew text? Instead it shows “???????” – Question marks.

Thanks.

Answer by Starx

During the data entry, or retrieval, the MySQL must be configured to accept unicode(UTF-8) data. There are number of ways, you can do this

  1. Place this line on the database connection part

    mysql_set_charset('utf8',$link); //$link is your connection identifier
    
  2. Run charset utf8 on CLI

  3. Use SET NAMES 'utf8' as your first query to the database

Moreover, while outputting such data the screen, the html should be configured to understand unicode characters as well. Use

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

At the top of the displaying page.

...

Please fill the form - I will response as fast as I can!