v-for based on element type

I'm facing an issue with a basic vue.js application. I have a list of referees and I need to iterate through that list and add data from an ajax request to each item. Strangely, when I try to display the list using v-for on a span element, it works perfectly fine. However, if I use a div instead of a span, it breaks. I'm curious as to why it breaks specifically with a div?

Here's a simplified example:

<html>
<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <div id="app">
        <p v-for="referee in referees">
            Name: {{referee.name}}<br>
            Match count: {{ referee.matches.length}}

            <!-- Using span here works perfectly -->
            <span v-for="match in referee.matches">{{match.date}}</span>

            <!-- But changing to div causes issues -->
            <!--div v-for="match in referee.matches">{{match.date}}</div-->
        </p>
    </div>
</body>

<script>
    let myReferees = [
        { name: "Referee 1", },
        { name: "Referee 2", },
        { name: "Referee 3", },
    ];

    $(document).ready(function () {
        var app = new Vue({
            el: '#app',
            data: {
                referees: [],
            },

            created: function () {
                for(let referee of myReferees){
                    referee.matches = [];

                    let matches = [ 
                        {date: "date1"},
                        {date: "date2"},
                        {date: "date3"},
                    ];

                    for(let match of matches){
                        referee.matches.push({
                            date: match.date,
                        });
                    }

                    this.referees.push(referee);
                }
            },
        });
    });

</script>
</html>

Check out this fiddle for a working demo: jsfiddle.net/vvu2f0m6/ The provided fiddle and code samples are functional, but switching from span to div will highlight the issue.

Answer №1

To ensure everything runs smoothly, make sure to follow this step: Replace the p tag with a div tag.

<body>
        <div id="app">
            <div v-for="referee in referees">
                Name: {{referee.name}}<br>
                Match count: {{ referee.matches.length}}

                <!-- If I use span here, everything works just fine -->
                <div v-for="match in referee.matches">{{match.date}}</div>

                <!-- But if I switch to div, it no longer works -->
                <!--div v-for="match in referee.matches">{{match.date}}</div-->
            </div>
        </div>
    </body>

Remember that you should not nest a div inside a p tag.

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

Optimal Approach for Managing Files in JavaScript

I have successfully uploaded a JSON file to my server using NodeJS and the 'http' module. Utilizing the NPM package 'formidable', I was able to save the file sent by the user onto the server. My next goal is to extract information from ...

How can we identify all the foreign_key1 ids from a MySQL join table that have a specific foreign_key2 assigned to them that is within a specified list?

I have a scenario where I have two tables, Table A and Table B, connected by a many-to-many relationship. Table A: ID --- 1 2 3 Table B: ID --- 4 5 6 7 Table AB: ID | A_ID | B_ID ---------------- 8 | 1 | 4 9 | 1 | 5 10 | 1 | 6 11 | 1 | 7 ...

Encountering issues with updating state object in setState function

Review the code snippet below: {split.participants.map(friend => { return <div key={Math.random()} className="form-check my-2 d-flex align-items-center justify-content-between"> <div ...

Is there a way to instantly remove script from the document head using jQuery instead of waiting a few seconds?

I currently have a setup where I am utilizing Google Maps in production. To make this work, I must include a script in the head of my document that contains the API key for the Google Maps JavaScript API that my application relies on. The API key is being ...

The submission of the form is not functioning correctly when triggered by JavaScript using a button

My website was designed using a CSS/HTML framework that has been seamlessly integrated into an ASP.NET site. Within a ContentPlaceHolder, I have implemented a basic login form. The unique aspect is that I am utilizing the onclick event of an image to subm ...

What is the best way to refresh a Windows 7 command prompt screen before executing a new function in Node.js?

I attempted system calls for cls and also tested out this code snippet: function clear() { process.stdout.write('\u001B[2J\u001B[0;0f'); } Unfortunately, none of the options seem to be effective. ...

Providing access to information even in the absence of JavaScript functionality

Is it possible to make the content of a webpage using jQuery/JavaScript visible even when JavaScript is disabled? Currently, I have a setup where clicking on an h2 header will display information from div tags using the jQuery function. I've made sur ...

Passing data between parent and child components within an Angular application using mat tab navigation

I am currently working on a project, which can be found at this link. Current Progress: I have implemented a mat tab group with tabs inside the app-component. When a tab is clicked, a specific component is loaded. Initially, most of the data is loaded in ...

Smooth transitions between points in animations using the D3 easing function

I'm working on an animated line chart that displays data related to play activities. I've been experimenting with changing the animation style between data points on the chart using d3.ease Currently, my code looks like this: path .attr("stro ...

Retrieving Firestore information as a JavaScript object

I'm working on retrieving data from Google Firestore as a JavaScript object in Vue.js (3). It functions correctly when used within a V-for loop, but I also need to utilize the array in methods. How can I convert the Firestore data into a basic JavaScr ...

Cloudflare SSL Error 522 Express: Troubleshooting Tips for Res

After setting up my express project using express-generator, I decided to make it work with a cloudflare SSL Certificate for secure browsing over https. My express app is running on port 443. Despite my efforts, when I try to access the domain, I encount ...

I find the JSX syntax to be quite perplexing

While examining some code, I came across the following: const cardSource = { beginDrag(props) { return { text: props.text }; } }; When working with JSX block code or building objects, I usually use {}. The cardSource variable in this co ...

Fix for fixed scrolling in the navigation bar

Having a website that receives traffic from different countries, including Portugal and other non-English speaking places, I decided to add a translation option. However, I encountered an issue with Google's translate feature which displays a banner a ...

The mouse coordinates do not align with the drawing of a rectangle on the canvas

I am having some issues with drawing a rectangle on mouse drag over the canvas that is overlayed on an HTML5 video JS player. The rectangle is being drawn, but it does not align correctly with the mouse coordinates. I suspect that the canvas, which is ove ...

How do I implement the use of "lengthMenu: [10, 25, 50]," within an if/else statement effectively?

Could someone help me with defining 2 different configuration lines for a datatable using if-else statements? I've tried writing the code but it doesn't seem to be working as expected. if(role === 1) { lengthMenu: [10, 25, 50], } else ...

Unable to make changes to the text within the textarea field

Currently, I am in the process of creating a script to streamline the tedious task of providing teaching feedback. To scrape data such as student names and classes, I am utilizing selenium/python. While everything is running smoothly, I have encountered an ...

Can html-webpack-plugin be configured to create <style> elements from CSS files?

I am managing a static site with Vue and Webpack. In my project, I have a file named style.css containing global CSS rules which I import using import './styles.css' in my index.js file. Additionally, I have some .vue files that generate their o ...

Guide for redirecting puppeteers' attention to a new pop-up interface

Currently, I am utilizing Puppeteer to carry out a test on a website. Upon clicking a button, a new popup browser window emerges displaying a report. Inside this new window lies data that I wish to extract. Is there a method for Puppeteer to switch its foc ...

Linking asynchronous AJAX requests using Angularjs

Currently in my AngularJS project, I have created a service with multiple functions that return promises. The AJAX Service I've Created: angular.module('yoApp') .factory('serviceAjax', function serviceAjax($http) { return ...

Text alignment issues cause animation to vanish

Utilizing particles.js, I set up a full-screen particle effect by specifying the animation to be full-screen with height: 100vh;. This worked perfectly as intended. However, when attempting to add text on top of the particle animation and center it vertica ...