0

I've got a Vue file and I would like to set an 'action' attribute of my form.

<template>
  <div>
    <form>
    </form>
  </div>
</template>

export default {
  created() {
    var test = document.getElementById("form");
    test.setAttribute('action', 'file.php');
  }

But setting it in a lifecycle hook doesn't work. What should I do?

3
  • You don't have an ID. Commented Feb 6, 2018 at 20:34
  • 1
    You should do this within Vue (bind the attribute), not by using the DOM directly. Commented Feb 6, 2018 at 20:35
  • Yeah, thanks it was unnecessary question, it worked! Thanks. Commented Feb 6, 2018 at 20:41

1 Answer 1

1

Couple of things:

  1. The mounted lifecycle hook is usually where you want to perform the initial actions for the component
  2. Using getElementById goes against the way Vue is supposed to be used

Something like this would make more sense:

<template>
  <div>
    <form :action='action'>
    </form>
  </div>
</template>

export default {
  data() {
    return {
      action: 'file.php'
    }
  },
  mounted() {
    // initialize things here
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.