0

Could you tell me how to add the text from text field to the array so it is displayed at the top of text field? Thanks

<div id="test">
    <p id="items"></p>
    <input type="text" id="color" />
    <input type="button" id="add" value="Add" />
</div>
<script>
    var theArray = new Array("Red", "Green", "Blue");
    var target = $("#items");
    for (var i = 0; i < theArray.length; i++) {
        target.append("<p>" + theArray[i] + "</p>");
    }
    $("#add").click(function () {});
</script>

1 Answer 1

2

Use

$("#add").click(function () {
    var color = $("#color").val()
    theArray.push(color);
    target.prepend("<p>" + color + "</p>");
});

DEMO

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

3 Comments

Since he want it on the top, use .prepend().
Thanks i will probably post more silly questions like it soon.
@user2976554, If you are putting effort we are ready to help.

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.