Exploring the magic of generating dynamic and raw HTML using VueJS

Is there a way to convert the span into an actual element? I am encountering an error with appendChild as it sees the variable as a string rather than an object. Any suggestions on how to resolve this?

export default{
    data(){
       ....
    }   
    methods:{
      update_period: function(event){
        var start = moment(event.start).format('M/D/Y'),
            end = moment(event.end).format('M/D/Y');
        
        var span = `<span @click="remove">{{ start }} - {{ end }}</span>`

        this.$refs.spans.appendChild(span);
      },
      remove: function(event){
        event.target.remove()
      }
    }
  }
<div ref="spans">

</div>

Answer №1

Here is an alternative approach to achieve the same outcome:

<template>
    <div>
        <span @click="clearDateRange" v-if="dateInterval">{{ dateInterval }}</span>
    </div>
</template>
<script>
export default {

    data() {
        return {
            dateInterval: null,
        }
    },

    methods:{
        updateDateInterval(event) {
            this.dateInterval = moment(event.start).format('M/D/Y') + ' - ' + moment(event.end).format('M/D/Y')
        },
        clearDateRange() {
            this.dateInterval = null;
        }
    }

}
</script>

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

transition from jQuery to Zepto

I have been utilizing multiple jQuery plugins in my codebase... Recently, I decided to switch over to Zepto, but encountered an issue Uncaught TypeError: Object function (a,b){return A.init(a,b)} has no method 'data' when checking the console ...

A guide to displaying JSON information within a data list element (dl)

I am facing an issue while trying to iterate through a multidimensional json data array. Each time I loop through the array, only the last element gets printed. var data = [ [{ "id": "67", "name": "Baby & Toddler Clothing ...

Secure Socket Layer (SSL) certification for hosting an HTTP server on an

I am currently facing an issue with insecure SSL certificates within my project that consists of two main components: An ESP32 IoT device with a HTTPS server A VUE2 + Vuetify PWA web application deployed on Firebase hosting. Imagine a scenario where a c ...

Implement a vertical scrolling animation in datatables

I am trying to utilize datatables to display all data. My goal is to have the data automatically scroll row by row every 3 seconds. This is my code, and you can also check it out on jsfiddle The intention is to showcase this data on a large screen. < ...

Experimenting with ES6 modules using Mocha

Recently, I was given a new project at work that involves using raw native imports/exports in code (compatible only with the latest browsers). The project is being worked on by consultants, and although I cannot make significant changes to their work, I am ...

Using Angular to Bind JSON Data

I'm currently in the process of evaluating different JS frameworks for a project and I find myself torn between Angular and Ember. As I continue to explore Angular, I have a specific question regarding data binding to an external json file stored on S ...

What is the best way to include attributes in an HTML element?

I've been researching how to dynamically add attributes to an HTML tag using jQuery. Consider the following initial HTML code: <input type="text" name="j_username" id="j_username" autocorrect="off" autocapitalize="off" style="background-image: lin ...

What is the function of the OmitThisParameter in TypeScript when referencing ES5 definitions?

I came across this specific type in the ES5 definitions for TypeScript and was intrigued by its purpose as the description provided seemed quite vague. /** * Removes the 'this' parameter from a function type. */ type OmitThisParameter<T> ...

Utilizing Vue.js for Streamlined Form Management: Adding and Editing with One Form

I'm attempting to utilize the same component for both adding and editing functionality in my application. Utilizing Firebase, I am checking if there is an id in the route parameters. If there is, it should render as the edit form; otherwise, it should ...

The getInitialProps function in Next.js React components does not automatically bind props

When building applications with Next.js, you have the opportunity to leverage a server-side rendering lifecycle method within your React components upon initial loading. I recently attempted to implement this feature following instructions from ZEIT' ...

JavaScript popup menu with a redirect URL

I utilized Tinybox from this source for launching a popup webpage. I am hoping that when I click the links on the webpage, the popup will close itself and redirect to the link's URL. Here are my JavaScript and HTML codes: <script type="text/java ...

The angular.copy() function cannot be used within angular brackets {{}}

Within my controller, I am utilizing the "as vm" syntax. To duplicate one data structure into a temporary one, I am employing angular.copy(). angular.copy(vm.data, vm.tempData = []) Yet, I have a desire to transfer this code to the template view so that ...

Executing synchronous animations in Jquery with callback logic

My jQuery plugins often rely on user-defined callbacks, like in the example below: (function($) { $.fn.myplugin = function(options) { var s = $.extend({}, options), $this = $(this); if (typeof s['initCallback'] = ...

The Axios POST request is not being properly parsed within a Python Flask application

Introduction Greetings! I have noticed that this particular question has been asked several times, but a suitable solution seems to be lacking. The Issue at Hand While GET requests are functioning as expected, there seems to be a problem when attempting ...

How can I stop Jquery Mobile from processing the URL hash?

Currently implementing Jquery Mobile in a preexisting web application is posing a challenge. The issue arises as Jquery Mobile is interpreting all URL hashes by default. For example: mysite.com/#foo In this case, the hash 'foo' is being directe ...

Launching the development server on a project created using Vue CLI 3

After successfully installing Vue CLI 3 globally on my Windows system using the command npm i -g @vue/cli, I proceeded to generate a project with the command vue create vue-project. I made sure to select all the necessary plugins as prompted during the se ...

Is it possible to use TypeScript in a React Native project with a JavaScript file?

Currently, I am learning React Native by working on app clones like Instagram and YouTube. I have recently started an AirBnb clone project, but I'm facing some issues with the initial build. One issue I noticed is that in 'App.js', the temp ...

Restrict Angular 2 Component to specific ancestor Component

Is it possible in Angular 2 to restrict a Component so that it can only appear within a specific parent element on a page? In other words, the Component should only be allowed if it has a certain parent element. Here is an example: Allowed: <parent> ...

Why won't my AngularJS Google Maps marker trigger any events?

My issue is with the marker event not working on a UI Google Map. I am using this link. Here is my view setup: <ui-gmap-markers models="mapResult" fit="true" idkey="mapResult.id" coords="'form_geo'" click="'onclick'" events="mapRe ...

An error handling event for XHTML compliance, similar to the HTML onerror event, is designed to address the issue of

Below is a piece of code that handles hiding the placeholder (imagecontent) for dynamically populated empty images and their captions: <head> <script type="text/javascript"> function hideEmptyImage() { document.getElementById(&q ...