Struggling to implement a variety of animated kebab menus with dropdown lists in Laravel 8 without success

If you require more information, feel free to ask. Thanks in advance. In Laravel 8, I have a button named "create app". When I click on it, it takes me to a page where I can add some data. After adding the data, it displays many apps in a list view. Each item in the list has an animated kebab menu. However, the issue is that when I create a new app, the dropdown menu only works for the last entered app. This means that if I create 10 apps, the dropdown menu will only work for the most recent one and not for the other nine.

Below is some code from my index.blade.php file that creates multiple kebab menus but does not function as a dropdown:

@foreach ($products as $product)
            <div class="col-xs-12 col-sm-12 col-md-12">
                    <form action="{{ route('products.destroy', $product->id) }}" method="POST">
                            <a class="btn btn-outline-info" href="{{ route('products.edit', $product->id) }}">
                                <br>
                                <img src="/logo/{{ $product->logo }}" width="100px" class="col-4">
                                <div class="col-2" style="color: #1d2124">
                                    <strong >App Name:- {{ $product->name }}</strong>
                                    <br>
                                    <strong>App Id:- {{ $product->id }}</strong>
                                </div>
                            </a>
                            <div class="pull-right">
                                <div class="kebab">
                                    <figure></figure>
                                    <figure class="middle"></figure>
                                    <p class="cross">x</p>
                                    <figure></figure>
                                    <ul class="dropdown">
                                        @csrf
                                        @method('DELETE')
                                        <li><button type="submit" class="btn btn-danger">Delete</button></li>
                                        <li><a href="api/json_link">All Json</a></li>
                                        <li><a href="api/SearchById">JsonById</a></li>
                                        <li><a href="api/SearchByName">JsonByName</a></li>
                                    </ul>
                                </div>
                            </div>
                    </form>
            </div>
@endforeach

Here is the JavaScript code from app.js which creates kebab menus for all apps but dropdown functionality only for the last inserted app:

require('./bootstrap');

require('alpinejs');
var kebab = document.querySelector(".kebab"),
    middle = document.querySelector(".middle"),
    cross = document.querySelector(".cross"),
    dropdown = document.querySelector(".dropdown");

kebab.addEventListener("click", function() {
    middle.classList.toggle("active");
    cross.classList.toggle("active");
    dropdown.classList.toggle("active");
});

Answer №1

document.querySelector will only return the final DOM element that matches your selector. To implement a dropdown menu for each kebab, you need to utilize

kebabs = document.querySelectorAll(".kebab")
...
kebabs.forEach(this.addEventListener("click", function(){
   //implement your desired functionality
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Align a Button in the Middle with Bootstrap?

Hey there! I'm working on creating a video list. I've utilized the bootstrap grid system to put it together. Here's the code I have so far: I'm trying to center the play button both vertically and horizontally within the thumbnail. Any ...

The functionality of returning false on ajax response does not effectively prevent the form from submitting

I'm encountering an issue where the return false statement doesn't seem to work when using an AJAX call. The form is still getting submitted successfully despite trying to prevent it with a conditional check on the response from the AJAX request. ...

Placing a group of input fields and a button based on the data-id attribute of the element

I have a form that appears when a button is clicked, and the save button saves input values into an object. Is there a more efficient way to write this function if I need to create 9 more buttons with different data-id attributes (e.g. data-id="2" and so ...

Avoid production build warnings in Vue.js without the need to build the code

Experimenting with vuejs, I decided to use it for a simple page even though I could have achieved the same without any framework. Now, my project is nearly production ready. The only issue is that it's just a single js and html file, but it shows thi ...

Using Node.js, we can create a program that makes repetitive calls to the same API in a

My task involves making recursive API calls using request promise. After receiving the results from the API, I need to write them into an excel file. Below is a sample response from the API: { "totalRecords": 9524, "size": 20, "currentPage": 1, "totalPage ...

Unexpected result produced by indexOf() function

When trying to display the index of the first element (violet) using an alert, I am getting -1 instead of the expected result. This unexpected outcome is hindering my progress in coding. As a newcomer to JavaScript, I'm facing this issue and seeking a ...

Using ng-options with the Add New feature is not compatible with the select statement

Hello everyone, I have some JSON values that look like this: [{"Employee":{"Emp_id":"EF00001","First_name":"Aruna","Last_name":"Jayalath"}}] Unfortunately, I am having trouble retrieving the values within the select statement when running this function i ...

What is the best way to incorporate a BigDecimal from Scala.js into my JavaScript project?

Presented below is an example of a Scala object: @JSExportTopLevel("Calculator") object Calculator { @JSExport def calculate(): BigDecimal = 3.14 } The exported singleton method can be invoked from a JavaScript application, but the result m ...

Unable to create canvas drawings using fingertips on mobile web browsers

Check out the code snippet below: canvas = document.getElementById("canvas"); ctx = canvas.getContext('2d'); tmp_ctx = element[0].getContext('2d'); element.bind('mousemove touchmove', function(event){ if(draw ...

How to troubleshoot Django's Jquery datatable aoData not functioning issue

Getting Started with Datatable Initial Data Setup object_list=[{'username': u'Paul', 'phone_number': u'9910087044', 'user_group__name': '', 'manager_name': ' ', 'role&ap ...

Having trouble resolving 'datatables.net/js/jquery.dataTables.js' in a Vue and React Project using the Surveyjs quickstart template

Currently, I am juggling two distinct projects - one utilizing Vue.js and the other employing React. Both projects stem from quickstart templates offered by SurveyJS. Upon diligently following the setup guidelines and executing the npm run start command, I ...

What is the best way to dynamically update a specific value within an object based on the current state in React/Next?

I have implemented a Context API where an object is set, and when the user clicks, the state changes. I need to update a specific value with the new state value. const initialState = { notification: false, setting: false, profile: false, } exp ...

Every time I submit the form, I aim to create a unique random string in the format of 'ZXCVBN'

Is there a way to automatically create a random 6-character string consisting of uppercase letters each time a form is submitted, and then assign this string to the 'code' parameter in the event array within the add-event.comonent.ts file? The g ...

Issue with Reddit example in Mozilla Addon SDK documentation on the second page

While developing my own addon, I encountered a problem that also occurred with the Reddit example. To simplify things, I will refer to the Reddit example in this explanation. When using the code provided in the example found here, this is the issue that a ...

Is there a keen eye out there that can help me pinpoint the mistake in this SVG/Javascript function?

I have created a series of SVG circles that are meant to alternate in color between blue and red with each click. However, I am experiencing some inconsistency in the behavior. In my local environment, the color doesn't change to red until the second ...

What are the steps to implement a personalized canvas in ThreeJS?

Struggling with creating a basic ThreeJS application that displays 3D text on the scene. The examples on the website are too complex for beginners like me. I'm finding it hard to make the scene appear without using tricks and I want to customize my ow ...

issue with mongoose virtual populate (unable to retrieve populated field)

During my project using mongoose with typescript, I encountered an issue with adding a virtual called subdomains to populate data from another collection. Although it worked without any errors, I found that I couldn't directly print the populated data ...

Use ng-model with ng-repeat to populate a dropdown selection

This particular issue is in reference to the jsfiddle link provided below: http://jsfiddle.net/n1cf938h/ Within the fiddle, there is an ng-repeat function generating a list of dates. My question pertains to identifying which dropdown option the user has s ...

In search of assistance in delivering HTML form data to a public HTTP service

Seeking assistance with forwarding html form data to a public http service. Any guidance or tutorial recommendations would be greatly appreciated! Below is the structure of my form: <span class="text"> <input class="text" type="text" name="First ...

What is the best way to iterate through multiple iframes?

I need help figuring out how to load one iframe while having the next one in line to be displayed. Is there a way to create a script that cycles through multiple iframes after a certain amount of time? ...