0

I have a html element textarea_01 to which I want to add a style when I press my button_01 which has an onClick function on it

myFunction: function(event) {
//some code that does something
};

Since I can't use directives with JSX I was thinking that the following will work:

<textarea id="textareaID" style={{resetWidth}}/>

And add to my function that's used on the onClick my code, so it will look like:

myFunction: function(event) {
var textarea = new Vue({
  el: '#textareaID',
  data: {
    resetWidth: 'width: 10px'
  }
})
//some code that does something
};

But it's not working and WebStorm tells me

You are using the runtime-only build of Vue where the template compiler is not available

How do I work around this and accomplish what I want to do, which is add a css property to a html element after I click on another element ?

Note: Webpack and the followin is used in the code github.com/vuejs/babel-plugin-transform-vue-jsx Note that almost all built-in Vue directives are not supported when using JSX, the sole exception being v-show, which can be used with the v-show={value} syntax.

3
  • 1
    Be aware that you are using the runtime-only version of Vue which expects any templates to be pre-compiled. Commented May 26, 2017 at 10:02
  • @varbrad how wo I pre-compile them ? Commented May 26, 2017 at 10:33
  • Either using vue-template-compiler, on just use the compiler version of VueJS. vuejs.org/v2/guide/… Commented May 26, 2017 at 11:20

1 Answer 1

3

You have to bind the style attribute to Vue, because you can't use moustaches on tag properties:

<textarea id="textareaID" :style="resetWidth" />

Because you are using runtime only Vue version, you should try with another approach.

Remove the moustaches from the textarea:

<textarea id="textareaID" />

Set the styles for the element with javascript on the mounted hook or another lifecycle hook that you want:

myFunction: function(event) {
    var textarea = new Vue({
        el: '#textareaID',
        data: {
            resetWidth: 'width: 10px'
        },
        mounted: function () {
            this.$el.setAttribute("style", this.resetWidth);
        },
    })
    //some code that does something
};
Sign up to request clarification or add additional context in comments.

15 Comments

error Parsing error: Unexpected token Doesn't even start
@Newcomer Which Vue version are you using?
"vue-router": "^2.1.1", with Vue 2
Make sure to use the Runtime+Compiler build of Vue, the runtime only build cannot compile your template markdown into render functions for you.
Take a look at what @varbrad says. My solution would work if you were using vue component files.
|

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.