When the `rendered` event is activated in VueJS along with "vue-markdown", the DOM may not be fully prepared yet

Here is the code snippet I am working with:

<vue-markdown :source="content" v-on:rendered="afterRenderContent"></vue-markdown>

Even after the afterRenderContent method is called, the HTML elements are not yet available.

I am looking for a way to manipulate DOM HTML elements after rendering, but I'm facing difficulties. How can I make sure the DOM is ready before making any changes?

I attempted to create a JSFiddle to demonstrate the issue, but it seems like the CDN library is not loading correctly:

Answer №1

Consider relocating your afterRenderContent method to the mounted lifecycle hook. It's important to only interact with DOM elements after the component has finished mounting.

mounted() {
    this.afterRenderContent()
}

To learn more about lifecycle methods, you can visit: https://v2.vuejs.org/v2/guide/instance.html#Lifecycle-Diagram.

Additionally, I see that you are incorporating some vanilla JavaScript in your JS Fiddle. You can utilize refs in Vue to access DOM elements in a more user-friendly manner.

<template>
    <h1 ref="header-one">Header One</h1>
</template>

// in your component method or mounted hook
mounted() {
    
    // access the header element 
    this.$refs['header-one']
}

UPDATE: Taking a closer look, it might be beneficial to connect to the mounted lifecycle method from a child component.

<vue-markdown :source="content" @hook:mounted="afterRenderContent"</vue-markdown>

For more insights on using refs in Vue, check out this informative blog post: https://blog.logrocket.com/how-to-use-refs-to-access-your-application-dom-in-vue-js/

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

Error: The middleware function is not recognized | Guide to Transitioning to React Redux Firebase v3

After utilizing these packages for my project, I encountered an error in middleware composition while creating a new react app with create-react-app. Below are the packages I have included. Can someone please help me identify what is missing here? HELP I ...

The retrieval of JSON file using $.GetJson() is unsuccessful from local directory

There is a basic autocomplete program in place. The data entered in the textbox is successfully captured while debugging, however, the GetJson() function fails to retrieve the JSON file, causing the program to malfunction. Here's the relevant code sn ...

The time on the X-axis is not displayed accurately

I have encountered an issue with the "Time data with irregular intervals" chart I am using. The x-axis data is in epoch time and should range from 12:00 PM to 3:00 PM. However, the problem is that the Highcharts x-axis labels are displaying a range from ...

AddDeviceModalCtrl is not defined as a function, it is showing as undefined

I am encountering the error mentioned above, I suspect that it is due to the controller not being properly attached to the module. However, I could be mistaken. Below is the definition of the controller. (function () { 'use strict'; angular . ...

What is the best way to send a React prop that is buried deep within a JSON structure?

Currently, I am in the process of creating a product table to showcase items for a store. The headers of this table include: id, title, imagePath, newPrice, oldPrice. To accomplish this, I have developed an ItemTable component within my React application t ...

Error message indicating that `vue-loader` could not be located when attempting to run `npm run

I keep encountering an issue when trying to run npm run dev. I'm getting the following error and have no idea how to resolve it. I've tried reinstalling Node.js to the latest version and reinstalling all packages via the command line: [webpack-cl ...

Utilizing a While Loop for SQL Queries in a Node.js Environment

So, I was attempting to iterate through an array using a while loop. I was able to successfully print a result from the SQL connection without the while loop, confirming that the query is working. However, when I tried to implement the same query within a ...

Retrieving values from a jQuery object array using keys rather than array indices

I am facing a challenge where I need to extract values from an object returned through $.post, but the order of the arrays can vary. Therefore, I must retrieve them based on their keys which are nested inside the array. An example is provided below. { Id: ...

FabricJS Canvas with a React DropDown Feature

While I have successfully created a TextBox on FabricJS Canvas, creating a Dropdown component has proven to be a challenge. The fabric.Textbox method allows for easy creation of text boxes, but no such built-in method exists for dropdowns in FabricJS. If y ...

How can the edit feature be implemented in AngularJS?

I am struggling with implementing an edit field in AngularJS. Can someone please help me with this? Here is the code for my CRUD operations: var app = angular.module('myApp', []) app.controller('myCtrl', ['$scope', functio ...

Choose a date range from the date picker

I am currently attempting to combine two dates using a rangepicker. Below is the command used to select the date: Cypress.Commands.add('setDatePickerDate', (selector, date) => { const monthsShort = [ 'janv.', 'févr.& ...

Learn how to incorporate an image into your Codepen project using Dropbox and Javascript, along with a step-by-step guide on having the picture's name announced when shown on the screen

Hey there, I'm in need of some assistance. I have a Codepen page where I am working on displaying 4 random shapes (Square, Triangle, Circle, and Cross) one at a time, with the computer speaking the name of each shape when clicked. I want to pull these ...

What is the reason behind RxJs recording 2 events during a long keypress?

I'm in the process of creating a user interface that reacts to keyPress events. Utilizing technologies like Angular and RxJS allows me to identify specific events. [Latest packages installed] The code structure appears as follows this.keyboard$ ...

Animation issue in Material UI autocomplete label feature

Hello, I am currently delving into the world of React and Material UI. I have been working on implementing the Material UI auto complete feature with chip functionality. You can see my progress here: https://codesandbox.io/s/runh6. Everything seems to be w ...

Every time I attempt to insert a background image into a div using jQuery, I am consistently faced with a 404 error message

When I hit enter in my search bar, a new div is created each time. However, I am struggling to assign a background image to the created div as I keep receiving a 404 error in the console. Below is the code snippet I'm working with: function appendToD ...

React-native horizontal sliding plugin

Can anyone recommend a reliable horizontal slider plugin for react-native? I've come across some options, but they haven't been working as smoothly as I'd hoped. ...

What is the best way to transmit JSON data to a Web API?

As I work on developing a website with web API integration, I encountered an issue while trying to send JSON data to my controller. Multiple actions were found that match the request: Post on type AuctionWebsiteASP.Controllers.MovesController readDatab ...

Tips for transferring information in JavaScript games

While browsing an HTML-based website, I am able to send POST and GET requests. However, in a JavaScript game like agar.io, how can similar actions be performed? Specifically, when playing a popular game like agar.io, how is my game state (such as positio ...

Exploring the DOM in JavaScript: Searching for the final ancestor containing a specific attribute

Check out this example of HTML code: <div id="main1" data-attribute="main"> <div id="section2" data-attribute="subsection"> <div id="nested3" data-attribute="sub-subsection"> </div> </div> </div> <div id= ...

Tips for retrieving javascript-generated HTML content

Currently, I'm attempting to retrieve article headlines from the NY Times website. Upon inspection, it appears that the HTML is being generated by Javascript since it's only visible when using the 'inspect element' feature in Firefox. ...