ItSolutionStuff.com

How to Get Element by ID in Vue JS?

By Hardik Savani • June 30, 2023
Vue.JS

I will teach you how to get element by id in vuejs app. this example will help you to get element by id using refs in vue js app.

If you are a using simple jquery and you require to change text or replace content of specific element then you can do it easily. In vue js you can't do it very simple, but you can do it using refs variable.

So, just see bellow example and learn about that:

Example:

<!DOCTYPE html>

<html>

<head>

<title>How to get element by id in vue js? - ItSolutionStuff.com</title>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

</head>

<body>

<div id="app">

<div id="myId" ref="myId">{{ message }}</div>

<button @click="myFunctionClick()">Click Here</button>

</div>

</body>

<script type="text/javascript">

new Vue({

el: '#app',

data: {

message:"Welcome"

},

methods:{

myFunctionClick: function () {

this.$refs.myId.innerText = 'Hello Bro';

}

}

});

</script>

</html>

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Vue JS Ajax Form Submit Example Code

Read Now →

Vue JS MultiSelect Dropdown Example

Read Now →

Vue Js Sweetalert Modal Notification Example

Read Now →

How to Use setTimeout Function in Vue JS?

Read Now →

How to Check Object or Array Empty or Not in Vue JS?

Read Now →

Laravel Vue Router Example From Scratch

Read Now →

Laravel Vue JS File Upload Using Vue-dropzone Example

Read Now →

Laravel Vue JS Axios Post Request Example

Read Now →

Laravel Vue JS Infinite Scroll Example with Demo

Read Now →

Laravel Vue JS Pagination Example with Demo

Read Now →

Dynamic Dependent Dropdown using VueJS and PHP

Read Now →

Laravel 5.6 - Dynamic Ajax Autocomplete using Vue.js

Read Now →

Vue JS Scroll to Element in Div using Vue-scrollto Example

Read Now →