2

I'm new at javascript and I need some help making sense of this code and why it's not working.

<html>
<head>
    <title></title>
</head>
<body>

    <form action="" method="POST">
        <input id="in" type="text" name="name">
    </form>
    <button id="add">addone</button>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
        $('#add').click(function() {
          $('#in').append('<br /><input type="text" name="name">');
        });

    </script>

</body>
</html>

I'm trying to append another html input but I have no ideas why it's not working!?.

1
  • An input has no children Commented Sep 30, 2014 at 19:00

1 Answer 1

4

An input is self-closing, it has no children, and you can't append to it.

You can however insert something after it

$('#in').after('<br /><input type="text" name="name">');

<html>
<head>
    <title></title>
</head>
<body>

    <form action="" method="POST">
        <input id="in" type="text" name="name">
    </form>
    <button id="add">addone</button>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
       $('#add').click(function() {
        $('#in').after('<br /><input type="text" name="name">');
    });

    </script>

</body>
</html>

Note that your duplicating the name, and depending on what you indend to do with it, using name[] could be a good idea ?

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

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.