0

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 ...

5
  • No need for (e.keyCode ? e.keyCode : e.which) jQuery already normalizes it with e.which Commented Aug 2, 2012 at 0:09
  • Can you please enlighten me? I'm very new here .. you mean I could just do (e.which) instead? Commented Aug 2, 2012 at 0:11
  • That's exactly what I mean, e.which is enough if you use jQuery. Commented Aug 2, 2012 at 0:11
  • is it right to call the function fnClickAddRow() ? because I did edit back to (e.which) and nothing seems to be happening .. sorry for being a pain in the ass :\ Commented Aug 2, 2012 at 0:16
  • It's fine, but you probably need to enclose the event in DOMReady too.. Commented Aug 2, 2012 at 0:18

1 Answer 1

3

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.

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

1 Comment

Funny .. I closed the browser and run back the codes and all went fine .. thanx guys!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.