After text is entered in the textarea i would like to target this class to let the user submit by pressing enter.
cmnt += '<td class="cmnt_save" data-id="' + id + '">Save Comment</td>';
Everything I've tried has failed miserably.. i've tried using jquery, JS and some inline JS too.
Here's my function with my tables:
// This function retrieves comments
function get_comments(id ,afterCtl ,ctl) {
var cmnt = '';
show_loading('.loading_box');
// Create the new row in the database - ajax returns json version.
var add_url = '/ajax/project_comment_list.php?id=' + id;
var slot = $.getJSON(add_url)
.done(function(data) {
// Draw the entire comment-block then display it
cmnt = '<tr class="cmnts">';
cmnt += ' <td colspan="11">';
cmnt += ' <table>';
cmnt += ' <tr class="cmnt_add">';
cmnt += ' <td class="cmnt_text">'
cmnt += ' <input type="text" id="cmnt_new" name="cmnt_new" rows="1">';
//cmnt += ' <textarea id="cmnt_new" name="cmnt_new" rows="1"></textarea>';
cmnt += ' </td>';
cmnt += ' <td></td>';
cmnt += ' <td></td>';
cmnt += ' <td class="cmnt_save" data-id="' + id + '">Save Comment</td>';
cmnt += ' </tr>';
$.each(data.ProjectComments, function (index, value) {
cmnt += add_comment_row(value.TEXT ,blank_if_undefined(value.EMP_INIT) ,value.ENTER_DATE);
});
cmnt += ' </table>';
cmnt += ' </td>';
cmnt += '</tr>';
remove_comments(ctl);
$(afterCtl).after(cmnt);
$('#cmnt_new').focus();
$(ctl).addClass('cmnt_active');
$(ctl).html(' - ');
//register the event
$('td.cmnt_save').click(function(){
add_comment($(this));
});
comment_shown = id;
})
.fail(function(jqxhr, status, error) {
hide_loading('.loading_box');
show_error('There was an error pulling the comments.');
});
//wire up the event
$('textarea#cmnt_new').keydown(function(e) {
if(e.which == 13) {
e.preventDefault();
alert('Cripes! You pressed enter!');
}
})
}
If anyone could be of any help to get this task accomplished or just recommend other/better ways of making this function correctly. Thanks in advance for the help.