0

Currently, Im using temperate literals to generate buttons to go along with locally stored data per result. This data is generated via a click which handles calling the storage to get back the information, displaying it a container. However, when I want to target the button, it's unresponsive.

My code

$(document).ready(() => {
    const genMonthlyQueueBtn = $("#genMonthlyQueue");
    const genTransientQueueBtn = $("#genTransientQueue");  
    

    const monthlyPlateQueue = () => {
        const $insertMonthly = $('#insertMonthlyPlates');
        $insertMonthly.empty();
        let monthlyPlates = JSON.parse(localStorage.getItem('Monthly Plates'));
        for (i = 0; i < monthlyPlates.length; i++) {
            console.log(monthlyPlates);
            $insertMonthly.append(`
            <div class="container" id="generatedMonthlyPlates">
            <input type="text" width="100px" name="monthlyLicensePlate" id="monthlyLicensePlate" class="licensePlate" placeholder="AB12345" value=${monthlyPlates[i]}/>
            <div class="text-center">
            <button class="btn btn-warning" type="submit" id="editMonthlyPlate" name="action">Edit
            <i class="material-icons right">edit</i>
            </button>
            <button class="btn btn-danger" type="submit" id="deletePlate" name="action">Delete
            <i class="material-icons right">delete</i>
            </button>
            </div>
            </div>
            `)
        }

    }    
    // get the array from local storage through queue function
    genMonthlyQueueBtn.click(() => {
        monthlyPlateQueue();
    })
    // call generated button and test functionality to edit and push back to storage
    const editMonthlyPlateBtn = $('#editMonthlyPlate');
    const editMonthlyPlate = () => {
        console.log('I was clicked');
    }
    editMonthlyPlateBtn.click(() => {
        editMonthlyPlate();
    })

Is there a better way to do this? or am I going about it the wrong way? This data is triggered with a previous button click, and when adding an onClick function to the button itself, It's just called every time I generate the list and not when I clicked the button itself which is expected but not what I need.

BlakeT
  • 59
  • 9
  • 2
    ID should be unique use Classes instead .. And for generated elements [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Mohamed-Yousef Apr 16 '22 at 01:35
  • `onclick` is not the same as a `click` event (onclick goes in the html ` – freedomn-m Apr 16 '22 at 10:27
  • so it should be btn.click(`#$monthlyPlate[i]`) {code}? – BlakeT Apr 18 '22 at 17:37

0 Answers0