0

Im using .append() to print the value of an input field to a span. The form is AJAX so multiple values are inputted into the form therefore I want to print these multiple inputs. This works well, apart from the below IF statement isn't switching the string value of 'names', any ideas?

$("input").bind("keydown", function(event) {
    var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
    if (keycode == 13) {
         var value = $(this).val();
        var names = '';
        if (names == '') {
            names = value;
        }
        else {
            names = value + ', ' + names '.';
        }
        $("span#name1").append(names);
        return false;
    } else  {
        return true;
    }
}
1
  • is names html or just a plain string? You probably want to use html() and not append(). Commented Mar 5, 2012 at 23:48

2 Answers 2

1

name == '' is always true in your code, and that's because it's the initialization value of your variable.

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

Comments

0
if (names == '')

Should be:

if (value == '')

That is my guess simply because you are setting names = '' in the preceding line so it will always be true.

2 Comments

But aren't I changing it's value to that of 'value' when I do 'names = value;' in the IF? Or does var have a fixed value?
You are, but you are then changing it back again just before the if.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.