[Vuejs]-How to retrieve text from a div on button press in Vue Native?

1👍

Using vue native you can simply do it this way

<view v-for="user in users" :key="user.id">
<button :on-press="()=> getDetail(user.number)" />
</view>

Then in your script

<scripts>
...
methods: {
getDetail(id){
this.number = id // assuming you already have this.number set in your data object
console.log(id)

}

}
<scripts />
👤dark

2👍

Here is an example with Vuejs3, but you should learn more:

<template>

<ul>
    <li v-for="user in users"
        :key="user.id"
        @click="showDetail(user)"
    >
      {{ user.id }} - {{ user.name }}
    </li>
</ul>

<div v-if="clickedUser">
    <h6>Detail</h6>
  <span>{{ clickedUser.id }}</span>
    <span>{{ clickedUser.name }}</span>
</div>
<script>
import {ref} from "vue";

export default {
    setup() {
        const users = [
            {id: 1, name: "A"},
            {id: 2, name: "B"},
            {id: 3, name: "C"},
            {id: 4, name: "D"},
        ];
        let clickedUser = ref(null);

        const showDetail = (user) => {
            clickedUser.value = user;
        };

        return {users, clickedUser, showDetail};
    },
};
👤hous

Leave a comment