The button is unresponsive to the cssStyleDeclaration within the DOM

I've been grappling with a seemingly simple bug for over an hour and could really use a fresh perspective to help me identify the issue.

In the code snippet below, I am attempting to change the style of two buttons. One button (deleteBtnToDecorate) responds correctly and changes its font size as expected when the function is called.


The other button, however, is proving to be elusive as its font size remains unchanged despite my efforts (doneBtnToDecorate):

 changeStyle: function(idNumber){
        const liToDecorate = document.getElementById(`id${idNumber}`)
        const doneBtnToDecorate = document.getElementById(`idDone${idNumber}`)
        const deleteBtnToDecorate = document.getElementById(`idDelete${idNumber}`)
      
        liToDecorate.style.textDecoration = 'line-through'
        doneBtnToDecorate.style.fontSize = '0.8rem'
        deleteBtnToDecorate.style.fontSize = '1.1rem'
      }

Things I've attempted:

  • Console logged clicking on both buttons to ensure correct targeting. All targets are accurate

  • Manually changed CSS font sizes to rule out Bootstrap issues (even though it would have affected both buttons if that were the case), but: Font successfully changes via CSS, not through DOM manipulation

This is my complete code:

 <ul class="item-list-ul">
        <li 
          v-for="(i, index) in items" 
          :id="`id${index}`" 
          :key="index">{{ i }}
          <div class="item-butons">
            <b-button :id="`idDone${index}`" class="done-btn" @click="changeStyle(index)" size="sm" variant="outline-dark">Hecho!</b-button>
            <b-button :id="`idDelete${index}`" class="delete-btn" @click="deleteItem(i)" size="sm"  variant="warning">Borrar</b-button>
          </div>
        </li>
      </ul>

  </div>
</template>

<script>
export default {
    name: 'TodoList',
    methods: {
      changeStyle: function(idNumber){
        const liToDecorate = document.getElementById(`id${idNumber}`)
        const doneBtnToDecorate = document.getElementById(`idDone${idNumber}`)
        const deleteBtnToDecorate = document.getElementById(`idDelete${idNumber}`)
      
        liToDecorate.style.textDecoration = 'line-through'
        doneBtnToDecorate.style.fontSize = '0.8rem'
        deleteBtnToDecorate.style.fontSize = '1.1rem'
      }
    }
  }
</script>

<style lang="css">
.item-wrapper{
    padding: 4vh 3vh 4vh 3vh;
}
.item-list-ul{
  margin-right: 4vh;
}
.item-list-ul li{
  margin-bottom: 3vh;
} 
.item-list-ul li{
  font-size: 1.4em !important;
  font-weight: 600;
  list-style: outside none none;
}
.done-btn{
  margin-right: 20px;
  font-size: 1rem !important;
  font-weight: 600 !important;
}
.delete-btn{
  font-size: 0.9rem;
  font-weight: 600 !important;
}


</style>

Answer №1

Your CSS is utilizing the !important declaration:

.done-btn {
  ...
  font-size: 1rem !important;
  ...
}

When you use !important, it will override any inline styles.

By the way, this approach is not the best practice in Vue. It's recommended to apply changes within the template using either class or style bindings instead of directly manipulating elements.

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

Simultaneously activate the 'onClick' and 'onClientClick' events on an ASP button using JavaScript

I have encountered an ASP button while working on existing code that has both onClick and onClientClick events attached to it. My goal is to trigger both events by generating a click event from an external Javascript file. The line of code I am using for ...

Tips for centrally zooming responsive images without altering their dimensions

I have created a custom jQuery and CSS function that allows an image to zoom in and out on mouseover while maintaining a constant box size. I modified an example code to suit my needs. Check out the demo here: https://jsfiddle.net/2fken8Lg/1/ Here is the ...

Unable to pass a $scope variable into an Angular filter due to interpolation error

In my Angular application, I have a filter that takes a user ID and converts it into a user image URL. It does this by checking if the ID exists in an array and returning the corresponding image URL from the array passed to the filter. The filter is fu ...

JavaScript Delayed Redirection

I'm attempting to launch opera from the command line and have it redirect to a page after 30 seconds. Here's what I currently have: C:\Programme\Opera\opera.exe -newpage javascript:function%20func1(){window.location.href='htt ...

Incorporating Jquery element loading animation for enhanced webpage experience

Currently, I am involved in a project where I aim to incorporate a fade effect on an element within the webpage. Upon loading the page, these elements are intended to appear with a subtle fade-in effect. Although attempting a basic method in jQuery such ...

Guide on implementing a progress bar for file uploads with JavaScript and AJAX request

I am looking to implement a progress bar in my file uploader. Below is the ajax call I have set up for both file upload and progress tracking. $(function() { $('button[type=button]').click(function(e) { e.preventDefault(); ...

Expandable fluid inline svg that dynamically fills the remaining space alongside text within a flex row

How can I ensure that an inline SVG occupies all available space within a row without specifying its height across different screen sizes? The solution involves setting the SVG to have 100% height with a viewBox, allowing it to adjust accordingly while ma ...

Having trouble aligning modal to the left in Bootstrap 4 and MVC 5

I am currently working on a project where I am using a modal window to display an iframe. The webpage within the iframe is quite wide, so I have adjusted the width accordingly. However, the modal itself is situated in the center of the screen, causing the ...

The Bootstrap accordion feature or show/hide function is experiencing issues when dealing with dynamically created elements in jQuery

When interacting with items in my sample application, a pop-up displaying the item's description should appear. However, attempts to use Bootstrap accordion or hide and show features were unsuccessful, only jQuery delegate event handlers worked. In g ...

React Native app utilizing Expo can successfully import local databases in a web view, but encounters issues when attempting to do so on smartphones

I created a small learning app and imported data from a local database using the following code: import { StatusBar } from 'expo-status-bar'; import { Platform, FlatList, StyleSheet, Text, View, TextInput, Button, SafeAreaView, ScrollView } from ...

Styling Angular with FullCalendar - Personalized CSS Design

I'm struggling with custom styling using FullCalendar for Angular. Specifically, I need to change the background color of the 'More Events' Popover, but no matter what I attempt, my styles aren't taking effect. I've been trying to ...

JQuery Ajax request functioning smoothly while native JavaScript falls short

I am currently troubleshooting two AJAX calls. One is written in native JavaScript while the other utilizes JQuery to call a PHP script. The jQuery AJAX call seems to be functioning properly, but I am encountering issues with the JavaScript version. Here i ...

What is a more efficient way to search and substitute text in NodeJS?

I am currently working on a task that involves reading a file, passing its contents and multiple arrays to a function, using regex to see if there are any matches, replacing them, and finally updating the file. The code I have put together may not be the ...

Image overlaying the full width of body content background

I am trying to find a solution to my problem, but so far no luck. The Body selector has a max-width of 85%, and there are other selectors like header which I want to have a background image that spans 100% of the width behind the body selector attribute. ...

Is there a way to dynamically replace a section of a link with the current URL using JavaScript or jQuery?

I have a link that appears on multiple pages, and I want to dynamically change part of the link based on the current URL* of the page being visited. (*current URL refers to the web address shown in the browser's address bar) How can I use JavaScript ...

Scroll the content within an iframe's page container

Is it possible to scroll the entire browser window to the top when clicking on a link inside the iframe I'm in? I have control over both the page container and the iframe. ...

Guidelines for implementing a seamless translation effect with CSS3

CSS: .horizon { position: absolute; top: 380px; width: 1800px; height: 50px; background: url(images/road.png) repeat-x; -webkit-animation-name: prop-600; -webkit-animation-duration: 20s; -webkit-animation-iteration-count: i ...

Initiate CSS animation upon modification of component properties

I am looking for a way to create a fade in/out effect on a component whenever its prop changes. Unlike basic examples that toggle styles based on boolean conditions, I want the element's visibility state to be updated directly through prop changes. F ...

Switching ng-Idle countdown time from seconds to minutes is possible by adjusting the configuration settings

I have implemented ng-idle in my application, where the warning popup appears after 10 seconds with the message: "Your session will be close in 10 seconds" However, I need to change this to minutes. The session should be closed in 5 minutes instead. How ...

What is the most efficient way to use the $slice operator on a highly nested array in mongoose

I am currently working on slicing a deeply nested array. To illustrate, consider the following structure. I aim to slice this array for pagination purposes. {messages: [{ message: { members: [ {example: object, blah: blah}, {example2: object2, blah2: blah ...