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

Error encountered: Application module "MyApp" not found

I have set up AngularJs and jQuery using RequireJs within a nodeJs framework. This is my main.js setup require.config({ paths: { angular: 'vendor/angular.min', bootstrap: 'vendor/twitter/bootstrap', jqu ...

Using Angular UI Router to Access $scope Beyond the Scope of the UI View

Looking for a way to access $scope outside the UI view in my todo app. The structure is simple, with three panels as shown in this design For the full code, visit here I need to access the to-do detail on the third panel using $rootScope, which currently ...

How to assign a specific class to a list item in AngularJS based on its attribute value

Having some difficulties with this code: .directive('mydirective', function() { return { template: '<ul>\ <li priority="500"><a href="#"><i></i>Inbox</a></li>\ ...

Guide to showcasing a story on Storybook

Overview I recently incorporated Storybook into my Nuxt.js application. I managed to get Storybook up and running successfully. I created index.vue and index.stories.ts. However, I'm facing an issue where Storybook is not displaying any Stories. ...

Code snippet for converting the current webpage into a PDF using Jquery

Looking for guidance on how to convert the current webpage into a PDF using client-side (JQuery) code. The PDF should include all content from the webpage along with a few images if possible. The main requirement is to have all webpage content in the PDF, ...

"Troubleshooting the ineffectiveness of JavaScript's

(After reviewing all questions and answers related to this issue, I have not found a solution that works for me.) Here is the code snippet in question: var timeoutHandle; function showLoader(show) { if (show) { $('.loader').html(&a ...

How to refresh an image in Next.js using setState even when the src path remains unchanged

Define a state variable: const [picture, setPicture] = useState(null); Assuming the picture is initially set to "123" and even after updating the image, the value remains "123". How can I reload the Image? <Image src={profileurl + picture} alt="profile ...

Can you guide me on implementing an onclick event using HTML, CSS, and JavaScript?

I am looking for a way to change the CSS codes of the blog1popup class when the image is clicked. I already know how to do this using hover, but I need help making it happen on click instead. The element I want to use as the button; <div class=&quo ...

Error encountered while adding x-ray-scraper to project using Npm

I am currently working on a Vue application and utilizing the x-ray-scraper library. However, when I attempt to run npm run serve in the terminal to preview the application locally, I encounter the following error: This dependency was not found: * _http_c ...

Validating dropdown lists with Jquery

Custom Dropdownlist: <div class="col-md-2"> <div class="form-group"> <label for="field-3" class="control-label">Priority</label> <select id="lstpriority" class="custom-selectpicker" data-live-search="true" da ...

"The Material-UI ListItem component acts as a link, updating the URL but failing to render the expected

Seeking help for the first time on this platform because I am completely perplexed. I'm working on coding a navbar for my school project website using Material-UI's List component within the Appbar component. However, I have encountered two issu ...

I'm having difficulty implementing a vue-awesome icon on my project

I am attempting to utilize the standard window-close icon from vue-awesome. I have imported my vue-awesome using the following code: import 'vue-awesome/icons'; import Icon from 'vue-awesome/components/Icon.vue'; Vue.component('i ...

Utilizing the Twitter API 1.1 to retrieve a list of tweets

As I work on updating my CMS component, I am incorporating integration with the Twitter API to fetch and showcase a list of tweets related to a user or search query. I have chosen to utilize the Twitter Restful API v1.1 as the 1.0 version is set to be disc ...

How can I keep the cursor from automatically moving to the beginning of an input field when the type changes?

Currently in the process of creating a Password field that allows users to hide or display their password in React/Ionic. I am aiming to maintain focus on the password field when switching the input type, similar to how it is done on the PayPal Login page ...

Can csrf protection effectively secure data being posted to a server via axios?

In my current project, I am utilizing Laravel but it is not a Vue Single Page Application (SPA) so no routes are being used. Instead, the registration, login, and other form interactions are done through modals using Vue. Axios is being used to send form ...

Arrange the items that are missing from Array B to be located at the bottom of Array A, organized in a file tree structure

I have two arrays containing different types of objects. Each object in the arrays has a title assigned to it. My goal is to compare these two arrays based on their titles and move any files that are not included in the bottom part of the fileStructure arr ...

Guide: Displaying components within a Vue2 string

Within my Vue2 component, I am working with a string that looks something like this: <template> <div><h1>Hello World</h1> <div v-html="testString"></div> </div> </template> <script> exp ...

Tips for creating an Enumerable 'similarity' search using linq.js

I have a JSON array that looks like this: [ { "_Id": "0001", "_PatentId": "0000", "_Text": "Employee", "_Value": "employee", "_PermissionLevel": 55 }, { "_Id": "0002", "_PatentId": "0000", "_Text": "Employees" ...

Error encountered when asynchronously iterating over an object in TypeScript

Could someone please help me understand why I am getting an error with this code? var promise = new Promise((resolve, reject) => { resolve([1, 2, 3, 4, 5]); }); async function doSomethingAsync() { var data = await promise; data.forEach(v = ...

Customize the focus function for an individual element

I am working on a custom component that needs to seamlessly integrate with the native blur and focus functions. My goal is to override these functions in order to achieve the specific functionality I need. Currently, I have managed to override the prototy ...