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?
- get the value of the input
- detect the "enter" key press
- insert a new row
- 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>
<tr><td>NumberVar</td><td>+$(input).val()+</td></tr>keyup, appendand table navigation using jquery