Search code examples
javascriptreact-nativevue.jsvue-native

How to retrieve text from a div on button press in Vue Native?


I have a list of user details inside a scroll view with each having a button. It looks something like as follows:

  1. User 1

  2. User 2

  3. User 3

The list is rendered using a v-for. On button pressed in each of the divs, I would like to extract some information, such as the User number. How can I approach this issue?


Solution

  • 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 />