0

I tried to pass a selected key to a function and set a value for it. Already Vue data has the key(htxt) and I passed the key on edit button event and get the passed key and tried to set prompt dialog's value to it but it's not working.

<html>
    <head>
        <title>Vue.js Table Edit</title>
        <!-- UIkit CSS -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/uikit.min.css" />

        <!-- UIkit JS -->
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit-icons.min.js"></script>
    </head>
    <body>
        <div id="Edit">
            <h3>{{ htxt }}<span uk-icon="icon: pencil" @click="onEdit('htxt')"></span></h3>
            <span>{{ stxt }}<span uk-icon="icon: pencil" @click="onEdit('stxt')"></span></span>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            var edits = new Vue({
                el: '#Edit',
                data: {
                    htxt: 'How to change h3 text?',
                    stxt: 'How to change span text?'
                },
                methods:{
                    onEdit: function(val){
                        console.log("passed val:",val)
                        var retVal = prompt("Enter your name : ", "your name here");
                        this.htxt = retVal;
                        // I passed key to hold the value in onedit function and 
                        // I tried to set value to that key like this.val = retVal
                    }
                }
                
            })
        </script>
    </body>
</html>

1 Answer 1

1

<html>
    <head>
        <title>Vue.js Table Edit</title>
        <!-- UIkit CSS -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/uikit.min.css" />

        <!-- UIkit JS -->
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit-icons.min.js"></script>
    </head>
    <body>
        <div id="Edit">
            <h3>{{ htxt }}<span uk-icon="icon: pencil" @click="onEdit('htxt')"></span></h3>
            <span>{{ stxt }}<span uk-icon="icon: pencil" @click="onEdit('stxt')"></span></span>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            var edits = new Vue({
                el: '#Edit',
                data: {
                    htxt: 'How to change h3 text?',
                    stxt: 'How to change span text?'
                },
                methods:{
                    onEdit: function(val){
                        console.log("passed val:",val)
                        var retVal = prompt("Enter your name : ", "your name here");
                        this[val] = retVal;
                        // I passed key to hold the value in onedit function and 
                        // I tried to set value to that key like this.val = retVal
                    }
                }
                
            })
        </script>
    </body>
</html>

var data =  {
  htxt: 'How to change h3 text?',
  stxt: 'How to change span text?'
}

// ok
console.log(data['htxt'])
console.log(data.htxt)

// error, You are using this method
// console.log(data.'htxt')

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much you made my day... Can you explain me what I did wrong?
@SagitSri I added instructions
val is a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.