0

Okay .. this is my first question here. Very new with javascript, so hopefully I'd be treated nicely (crossing fingers) ... I have a html code down here:

<table width="500" border="1">
  <tr>
    <td>No.</td>
    <td>Name</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<br />
Enter Name: 
<input type="text" name="name" id="name" />

I'm trying to have whatever value I enter in the input "name" to go directly as a new row in the html table attached with a running number (1,2,3,etc) in the "No." column using the "Enter" key instead of clicking on buttons. At the same time the value in "name" will be cleared and focused as to ready for the next value entrance.

4
  • High level steps: On 'keyup' check if it's the Enter button pressed (===13) if so, get the No. in the last row, increment it and append the following to the table <tr><td>NumberVar</td><td>+$(input).val()+</td></tr> Commented Aug 1, 2012 at 19:09
  • Owh .. I'm sorry .. I don't know where to start .. been searching for the nearest example all over the site and found no resemblance at all as a solution Commented Aug 1, 2012 at 19:10
  • The above comment should help you figure out the methods in jQuery yourself - look for keyup, append and table navigation using jquery Commented Aug 1, 2012 at 19:10
  • okay .. lemme try this at jsfiddle and post it over for reference .. thank you :-) Commented Aug 1, 2012 at 19:13

3 Answers 3

5

Jeebison,

Being new to javascript I'm going to phrase this more as a tutorial than the exact answer.

First things first- What are the steps that need to occur to make this happen?

  1. get the value of the input
  2. detect the "enter" key press
  3. insert a new row
  4. set the row to the value of the input

Ok, now we've dissected each the problem into lots of small tasks. This makes it much easier to look for answers on each of the subsections of the problem?

Next- Do you know about javascript frameworks?

When writing javascript many browsers handle parts of the standard differently or certain parts aren't supported at all. As a result, writing javascript entirely by hand is not only cumbersome, but difficult to write cross-browser. Frameworks make development a lot easier and cleaner because they are built to handle the same functions across all browsers.

http://jquery.com -is my favorite and very easy to use so I will give you examples using that.

Ok, onto the coding.

First thing, we need to link the framework to the html page. If it's a small site or you're playing around I like to have google host the jQuery for me. Normally you might just download it.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

To actually add code and make use of jQuery we need to add some script tags and wait for the DOM (Document Object Model)to be ready on the page.

<script type="text/javascript">
$(document).ready(function() {
   // do stuff when DOM is ready
});
</script>

Now we need to select the element that we're trying to get data from. jQuery selectors are perfect for this. A simple example is grabbing an element by its "id" attribute.

In the case of the input we'll use "name". When referencing an ID in a selector, we always precede it with a #.

$('#name').val() 

This will give us the value of the input. Now add another row.

Your table doesn't have an ID, but lets give it one.

<table id='numbers'>

add the html for the row

$('#numbers').append('<tr><td></td><td></td></tr>');

select the first column of the row we just added and add the number.

var rows = $('#numbers tr').length;
$('#numbers tr:last td:first').html(rows);

copy the value of the input to the second column

$('#numbers tr:last td:last').html($('#name').val());

remove empty the input

$('#name').val('');

Now to detect the keystroke we will attach a listener to the input and place all of this code inside.

$('#name').on('keyup', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        $('#numbers').append('<tr><td></td><td></td></tr>');
        var rowcount = $('#numbers tr').length;
        $('#numbers tr:last td:first').html(rowcount);
        $('#numbers tr:last td:last').html($('#name').val());
        $('#name').val('').focus();
    }
});

This looks good, except $('name') is making extra selections slowing down your code and its redundant inside of a function attached to that object. We can use $(this) instead of $('#name') inside of the listener which references the initial selection done by

$('#name').on('keyup', function(e) {

So we have -

<script type="text/javascript">
$(document).ready(function() {
   $('#name').on('keyup', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        $('#numbers').append('<tr><td></td><td></td></tr>');
        var rowcount = $('#numbers tr').length;
        $('#numbers tr:last td:first').html(rowcount);
        $('#numbers tr:last td:last').html($(this).val());
        $(this).val('').focus();
    }
   });
});
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Kosmonaut, thank you .. I'm now engaging the gears slowly now :-)
This is EXCELLENT material!
5

I'll throw you a bone here since this is pretty simple code (and you're new). Try this jsFiddle example.

HTML

<table width="500" border="1">
  <tr>
    <td>No.</td>
    <td>Name</td>
  </tr>
</table>
<br />
Enter Name: 
<input type="text" name="name" id="name" />​

jQuery

var index = 1;
$('input[name=name]').on('keyup', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        $('table').append('<tr><td></td><td></td></tr>');
        $('table tr:last td:first').html(index);
        $('table tr:last td:last').html($(this).val());
        $(this).focus().select();
        index++;
    }
});​

4 Comments

Oh my .. that was fast .. right .. I'll work the focus and purging the text input from there .. Thank you, sir!
To purge the text change this line $(this).focus().select(); to this $(this).focus().val('');
j08691 .. you're just too kind .. thanx again!
No sweat. We were all new here once.
0

Welcome to Stackoverflow.

If you're using jQuery, check out jQuery's API for detailed information about what functions are available. Your specific question seems to be around events - which you can learn more about here.

In short, you'll want to bind an event that delegates the result to another control. I suggest looking at the keyup function.

I hope that helps.

References:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.