Having trouble implementing an onClick event to change a button's CSS to href in Vue.js

When working with a SinglePageApp and using UIKit (UKit), I have found that I need to use a <button> tag instead of an <a> tag.

Previously, the <a> tag was written like this:

<a type="button" class="uk-button uk-button-link" href="#events">Events</a>

However, using the <button> tag in this way did not produce the desired result.

<button @click="location.href='{{ url('events') }}'" class="uk-button uk-button-link">EVENTS</button>

Please note: An ESLint error is being displayed stating '[js] ';' expected'.

Answer №1

Your solution is on point, but unfortunately, it's not performing as expected in a single-page application due to an undefined error with scopelocation.

Therefore, I've come up with an alternative method:

      <button @click="goToEvents()" class="uk-button uk-button-link">EVENTS</button>

and

methods: {
    goToEvents: function () {
        location.href='#events'
    }
},

Answer №2

If you want to implement an onclick event with Vue, here's how you can do it:

<button v-on:click="location.href=url('events')" class="uk-button uk-button-link">EVENTS</button>

Another way to achieve the same result is by using

@click="location.href=url('events')"

Check out Vue Event Binding for more detailed information.

If you are working within a form and wish to prevent the form submission when clicking the button, you should include .prevent, like so:

v-on:click.prevent="url('events')"

Remember to use {{ }} (mustache) syntax when rendering text content. For dynamic values in attributes, use the colon prefix :. For example, to assign a dynamic value to a data attribute, you would write

:data-something="myDataProp"

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

Modifying the color of a placeholder in an HTML input using CSS

Version 4 of Chrome now supports the placeholder attribute on input[type=text] elements (and likely other browsers do too). Despite this, the following CSS code does not affect the color of the placeholder text: input[placeholder], [placeholder], *[pla ...

Aligning Content in the Center of a CSS Grid

Having difficulty with a CSS Grid layout that should consist of 7 squares per row to create a calendar style design. The layout seems fine, but I am struggling to center the title and subtitle in the middle of each item. I have tried using margin:auto, ver ...

Enhancing User Experience on Android Devices: Automatic Website Zoom-In Feature for Responsive Design

For the first time, I am delving into creating a responsive website design. I seem to be encountering difficulties when it comes to smartphones. I have been using the following meta-tag to ensure that the website loads "zoomed in". (I found this method on ...

The program encountered an error because it was unable to access the overview property since it was undefined

I encountered the error below Cannot read property 'overview' of undefined I am struggling to understand what is causing this issue. <template> <tr v-for="(row, fold) of perFold" :key="fold"> <td>{{ ...

Transform the appearance of a button when focused by utilizing CSS exclusively or altering it dynamically

As a newcomer to coding, I am currently working on a project that involves changing the color of buttons when they are clicked. Specifically, I want it so that when one button is clicked, its color changes, and when another button next to it is clicked, th ...

Error: Failed to load chunk (VueJS + Webpack) - Please try again

Currently, I am working on a project that involves Wordpress and VueJS 3 with a Webpack 5.43 build. Whenever a new version is deployed on the production environment and a visitor already has a tab open on the website, I encounter the following error messag ...

Creating a Select component in React without relying on the Material-UI library involves customizing a dropdown

Is there a way to achieve a material UI style Select component without using the material UI library in my project? I'm interested in moving the label to the top left corner when the Select is focused. export default function App({...props}) { retu ...

Unleashing the power of Sass and CSS in your Next Js project

Trying to incorporate sass and css modules into my next.config.js has been challenging due to an error I keep encountering: Failed to compile. ./node_modules/@riskalyze/react-ui/node_modules/@riskalyze/calendar/assets/index.css Unknown word (1:1) > 1 ...

Tips for splitting a container of specific height into sections measuring 80% and 20%

I am working on a container with a fixed position that I want to split into two halves: 80% and 20% at the bottom. This is what I want it to look like: Click here to see the image. Note: The layout should adjust itself when the window is resized. You c ...

Investigating Jquery Flip Card Issues

Looking to create a set of flip cards using HTML, CSS, and jQuery. Currently facing an issue where only the first card is flipping when clicked. Any suggestions on how to modify the jQuery code to make it work for all cards would be highly appreciated. C ...

What is the best way to ensure that the larger child divs always fit perfectly within the parent div?

Creating a Responsive Layout <div class="container"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> CSS Styling .box1, .box2, .box3{ display: block; f ...

Search field in DataTables appears to be misaligned

I'm in the process of developing a small website using JSP and DataTables (currently only for the first table). Here's what I have so far: As you can observe, there seems to be an alignment issue with the search field position. I'm n ...

I would like to terminate the property when processing it on a mobile device

<div class="col-sm-24 embed-responsive"> <iframe width="100%" src="http://www.youtube.com/embed/${item.snippet.resourceId.videoId}"></iframe> </div> .embed-responsive iframe { height: 185px; margin-top: -4%; paddin ...

Tips for adjusting the height of a Safari extension during runtime

After successfully developing a Safari extension, I am facing an issue with adjusting the width and height of the popover dynamically at runtime. Can anyone provide insights on how to modify the dimensions of the popover based on its content? ...

Troubleshooting ASP.NET Content Page Error: jQuery Object Expected

Currently working on designing a personal ASP.NET Web page, I have encountered an issue with making a sticky div using jQuery. Despite all my other jQuery functions functioning properly, the sticky div seems to be causing trouble. stickydiv.js $(document ...

loop not functioning properly with file type input

Having trouble uploading an image and copying it into a folder named images within a loop. Can you assist with solving this issue? Here's my code: $sql="SELECT * FROM product"; $q=$conn->query($sql); while($r=$q->fetch(PDO::FETCH_ASSOC)) { $cod ...

Building a custom Vuetify extension to enhance core features

I am currently working on developing a plugin-based component library to ensure consistency across various product lines using vuetify. However, when I try to install the library and add the components, I encounter multiple errors related to the dark theme ...

The Art of Validating Forms in Vue.js

Currently I am in the process of developing a form with validation using Vue, however, I've run into some errors that are showing up as not defined even though they are currently defined. HTML <form class="add-comment custom-form" @submit="checkF ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

Struggling to properly send props to the child component in Vue 3

Is there a way to pass data from the request through axios in the root component to the child using Vue? Currently, only the "title" field is displayed correctly, but I also need to output the "body". Note: This is my first time working with Vue and I&apo ...