Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Textfield entries into an array

I would like to add the contents of a textfield to an array on the click of a button. I would also like the array to store the number of times each word has been written and display the top 10 in labels. I would then like another button to check and update the labels. I would also like this to be stored. How would I do this?

2 Answers

I think this addresses the first part of your question. I am not sure about the other parts though. Hope this helps!

<!docktype html>
<html>
<head>
    <title>Textfield entries into an array</title>
</head>
<body>
    <div id='container'>
        <form>
            <label for="test">Values for myArray</label><br>
            <textarea name="test" rows="10" cols="30" id="test"></textarea><br>
            <input class='submit' type="button" value="Submit Value to Array">
        </form>
    </div>

    <script type="text/javascript" src='js/jquery.js'></script>
    <script type="text/javascript">
        var myArray = new Array();


        $('input.submit').click(function(){
            var content = $('#test');
            var lastIndex;

            if ($.trim(content.val())){   //checking if there is something written in the field
                myArray.push(content.val());
                lastIndex = myArray.length -1;
                content.val(''); //resets the value of the textarea to nothing
                alert(myArray[lastIndex] + ' - there is ' + myArray.length + ' item(s) in the array');
            }
        });

    </script>
</body>
</html>

Sorry. It is for iOS.

Gah, guess I missed that tag. Sorry >.<