Creative

How to build an array with Javascript

Building an array in Javascript and filling a text area with the array and clearing it on button click.

CodePen example.

To build an array in Javascript you need to have defined the array and then use push() to insert a value at the end of the array.

Javascript array push example:

let the_array = [];
the_array.push('itemValue');

Clearing the array is done by setting the array back to empty: fruit_array = [];

Now a system to add values from a button into an array which will then populate the text area in its array form:

The jQuery:

let fruit_array = [];

$(document).ready(function () {

    $(".add-btn").click(function () {
        var fruit = $(this).text();
        fruit_array.push(fruit);
        $('#array-text-box').val(JSON.stringify(fruit_array));
    });

    $(".clear-btn").click(function () {
        $('#array-text-box').val('[]');
        fruit_array = [];
    });

});

The HTML:

<div class="container">
    <div class="card">
        <div class="card-header">
            <h1 class="text-center">Build an array</h1>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-6 col-md-2">
                    <button type="button" class="btn btn-success btn-sm add-btn">Apple</button>
                </div>
                <div class="col-6 col-md-2">
                    <button type="button" class="btn btn-primary btn-sm add-btn">Banana</button>
                </div>
                <div class="col-6 col-md-2">
                    <button type="button" class="btn btn-success btn-sm add-btn">Lemon</button>
                </div>
                <div class="col-6 col-md-2">
                    <button type="button" class="btn btn-primary btn-sm add-btn">Lime</button>
                </div>
                <div class="col-6 col-md-2">
                    <button type="button" class="btn btn-success btn-sm add-btn">Orange</button>
                </div>
                <div class="col-6 col-md-2">
                    <button type="button" class="btn btn-primary btn-sm add-btn">Melon</button>
                </div>
            </div>
            <div class="row my-3">
                <div class="col-12">
                    <textarea class="form-control" id="array-text-box" rows="3">[]</textarea>
                </div>
            </div>
            <div class="row">
                <div class="col-12 text-center">
                    <button type="button" class="btn btn-danger btn-lg clear-btn">Clear the array</button>
                </div>
            </div>
        </div>
    </div>
</div>

 

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago