Jeebsion
Jeebsion

Reputation: 353

Execute jquery function upon enter key

Okay .. this is my attempt to execute a function instead of calling from it from the tag .. thing is .. it's not working .. mind you I'm still fresh with all these sigh

        <div class="widget first">
                <div class="head"><h5 class="iFrames">Static table</h5></div>
            <table id="example" cellpadding="0" cellspacing="0" width="100%" class="tableStatic">
                <thead>
                        <tr>
                        <td width="20%">Column 1</td>
                        <td width="20%">Column 2</td>
                        <td width="20%">Column 3</td>
                        <td width="20%">Column 4</td>
                        <td width="20%">Column 5</td>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>

<input type="text" name="name" id="name" />

and the script would go:

/* Global var for counter */
var giCount = 1;

$(document).ready(function() {
   $('#example').dataTable();
} );

function fnClickAddRow() {
    $('#example').dataTable().fnAddData( [
        $("#name").val(),
        giCount+".2",
        giCount+".3",
        giCount+".4",
        giCount+".5" ] );
     $("#name").val('').focus();
    giCount++;
}

$('#name').on('keyup', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
       fnClickAddRow();
    }
});

Now, I'm trying to add new row with the value of "name" from the input upon entering. And it's not working :\ .. by the way in this example I'm using DataTables ...

Upvotes: 0

Views: 2089

Answers (1)

Pedro L.
Pedro L.

Reputation: 7536

It works for me:

http://jsfiddle.net/eCrJc/1/

Try to put fnClickAddRow() and $('#name').on('keyup' inside $(document).ready and check your browser's console for any javascript errors.

Upvotes: 3

Related Questions