VUE- the GetElementByClassName function is malfunctioning


I attempted to utilize 'getelementbyclassname' within VUE methods. My reason for doing so is that

instead of applying information using :style, I would like to adjust the width of the div where I applied my class named 'classon'. If I were to link the style directly to the div, it wouldn't allow for interactivity. Therefore, in order to achieve a level of interactivity, I must access the DOM within VUE.

How can I access DOM element information in VUE?

More specifically, this part does not function as expected

  methods: {
    growit(){
      let vueele=this.$el
      vueele.getElementsByClassName('classon').style.width='300px'
    },

The complete code is displayed below.

-HTML-

<script src="https://npmcdn.com/vue/dist/vue.js"></script>

<div id="exercise">
  <!-- 1) Start the Effect with the Button. 
    The Effect should alternate the "highlight" or "shrink" class 
    on each new setInterval tick 
    (attach respective 
    class to the div 
    with id "effect" below) -->
  <div>
   ...
   

</div>
<script src='./app.js'></script>
<style>
 ...
 
</style>

-JS-

new Vue({
  el: '#exercise',
  data: {
    classboolean:true,
    ...

  },
  methods: {
    growit(){
      let vueele=this.$el
      vueele.getElementsByClassName('classon').style.width='300px'
    },
    startEffect() {
    this.classboolean=!this.classboolean
    },
    startProgress(){
     ...
      
    },

  }
});

Your support is always greatly appreciated.

Answer №1

let totalWidth = 0; 

new Vue({
  el: '#exercise',
  data: {
    areClassesVisible:true,

    firstClass:{
      'background-color':"red"
    },

    secondClass:{
      'box-shadow':"grey 2px 2px 2px 2px",
      'margin':'50px',
    },
    newclass:'',
    anotherclass:'',
    booleans:'',
    width:'',
    rectselection:'',
    boolean1:false,
    boolean2:false,
    boolean3:false,
    boolean4:false,
    boolean5:false,




  },
  methods: {
    increaseWidth() {
      totalWidth += 50;
      document.querySelector(".btn-primary").style.width = totalWidth + "px";
    },
    toggleEffect() {
    this.areClassesVisible=!this.areClassesVisible
    },
    startProgressBar(){
      console.log

      if(this.rectselection==1){
        this.boolean1=true;

        this.boolean2=false; this.boolean3=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==2){
        this.boolean2=true;

        this.boolean1=false; this.boolean3=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==3){
        this.boolean3=true;

        this.boolean1=false; this.boolean2=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==4){
        this.boolean4=true;

        this.boolean1=false; this.boolean2=false; this.boolean3=false; this.boolean4=false;
      } 
      else if(this.rectselection==5){
        this.boolean5=true;

        this.boolean2=false; this.boolean3=false; this.boolean4=false; this.boolean1=false;
      } 

    },

  }
});

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

The GrandchildComponent in a Vue child component should be responsible for updating a list within the ParentComponent

I have a DocumentView.vue component which contains a DocumentListingView.Vue component. The DocumentListingView.vue further includes a DocumentItem.vue component. Within the DocumentItem.vue component, the corresponding document can be deleted through an A ...

Retrieve anchor elements from multiple HTML documents

I have a challenge that I'm not certain is achievable, but I am attempting to extract all anchor tag links from several HTML files on my website. Currently, I have developed a PHP script that scans various directories and subdirectories to create an a ...

Node.js is known for its ability to export both reference and pointer values

Having an issue with exporting my routing table from my express application. In the /bin/www.js file, there is a global variable representing the routing table: var routingTable = []; To create a simple route, new routing objects are pushed using the se ...

jQuery - contrasting effects of the before() and after() functions

I'm working with a sortable list that is not using jQuery UI sortable, but instead allows sorting by clicking on buttons. Sorting to the right using after() works perfectly, however, sorting to the left with before() is causing issues. First, here&ap ...

Scripts in iframes within webviews are not preloading and executing properly

When using the <webview> tag in the renderer process within the <body> of a web page, the following code is used: <webview src="http://somewebpage.com" preload="somescript.js"> The script somescript.js will be execute ...

Troubleshooting a setTimeout filter problem in Vue

Implementing a notification system in Vue has been my latest project. I've created a Notifications component to store error messages that I want to display. data(){ return { alerts: { error: [] } ...

Despite implementing an event listener, the Google Maps API is failing to resize properly when created using AJAX

Currently, I am facing an issue with the Google Maps API as the map generated is not resizing to fit the div. Instead, it shows a large grey area which is quite frustrating for me. Can someone please assist me in resolving this problem? Below is the code ...

The ZIP file downloaded from NodeJS is corrupted and cannot be opened

Currently, I am utilizing the following code to create a MySQL dump in memory, then zip that SQL file with a password from memory and save it to the hard drive so it can be streamed to the client... /* DUMP - database */ var mysqld ...

Step-by-step Guide to Setting pageProps Property in Next.js Pages

When looking at the code snippet provided in .../pages/_app.js, the Component refers to the component that is being exported from the current page. For instance, if you were to visit , the exported component in .../pages/about.js would be assigned as the ...

Vim: Turn off autocomplete when using insert mode key bindings

I've set up a mapping in insert mode to automatically indent brackets: inoremap [;<CR> [<CR>];<Esc>O<Tab> When I use it, the result looks like this (the pipe character represents the cursor): const a = [ | ]; Now, I want ...

Can you provide me with instructions on how to create a toggle effect for a button using vanilla JavaScript?

Looking for guidance on creating a toggle effect with a button that switches between 2 images. I've managed to get it working with event listeners on btn2 and btn3, but can't seem to implement the 'toggle' effect on btn1. Any insights o ...

Utilizing Translation (i18n) in AngularJS Controller to Implement Popups

I'm currently working on an AngularJS app and need to implement i18n. Everything is running smoothly except for error handling, specifically with utilizing translations in controller for popups. Here is a snippet of my controller: function showError ...

What is the best way to style the current element being targeted in React?

In my React application, I am trying to dynamically adjust the height of a textarea element based on its content. I want to achieve this by utilizing an 'onchange' listener to trigger a resize function. While I have successfully implemented the ...

In what way can a container impact the appearance of a child placed in the default slots?

Visiting the vue playground. The main goal is for the container component to have control over an unspecified number of child components in the default slot. In order to achieve this, it's assumed that each child component must either hold a propert ...

Navigate to the editing page with Thymeleaf in the spring framework, where the model attribute is passed

My goal is to redirect the request to the edit page if the server response status is failed. The updated code below provides more clarity with changed variable names and IDs for security reasons. Controller: @Controller @RequestMapping("abc") public clas ...

After signing out, the function getRedirectResult in Firebase is being invoked

I'm a bit confused about some interesting behavior I've noticed while working with Firebase. Although I never used the older version, I believe getRedirectResult is a newer feature since they partnered with Google. Currently, I am using Vue.js a ...

Is it possible in Javascript to verify if a different script has been triggered?

I recently created a pop-out menu for a website using HTML and Javascript. The menu currently has a button that opens a div container with a close button inside it. While the buttons are functioning properly in hiding the div, the elements within the div d ...

Using AngularJS to dynamically populate a dropdown menu from a JSON object

I am a beginner to Angular and currently facing my first significant challenge. The JSON object below is the address data retrieved from a third-party API and added to $scope.AddressData in a controller: $scope.AddressData = [{ "Address1":"South Row", ...

The scale configuration for scale: x is not valid for creating a time scale chart using chart.js

I am currently utilizing VueJS and attempting to integrate a time scale chart using Chart.js. However, I encountered the following error: Invalid scale configuration for scale: x Below is my configuration : First, I have a component named Chart.vue with ...

Having difficulty completing the text field and submitting it automatically

My goal is to automatically fill in the "Reason for Access" text box with the word "TEST" using Tampermonkey. I am new to using Tampermonkey and UserScript, so I appreciate your patience. Currently, I am facing an issue where the "Reason for Access" field ...