0

I'm using vue 2. I have a text get from api.

"Hello everyone! My name is [input]. I'm [input] year old".

Now, I have to replace the [input] with an html input and handle the onKeyUp for this input.

What I have to do?

I used computed render html, but it not work with v-on:xxx.

content.replaceAll('[answer]', '<input type="text" class="input_answer" v-on:click="handleInput()"/>')

Thanks!

5
  • you mean when some inputs in the input tag then that value should replace the [input] in this string "Hello everyone! My name is [input]. I'm [input] year old" on keyUp event? Commented Jul 20, 2022 at 16:25
  • 1: I'm have a string "Hello everyone! My name is [input]. I'm [input] year old" get from api. 2: I have to replace the [input] with an html input. 3. When user text into input. I can get value from user text. Ex: User text: Hello everyone! My name is Jerry. I'm 5 year old. I'm get name => jerry and old = 5. Commented Jul 20, 2022 at 16:41
  • okay what do you mean by this? content.replaceAll('[answer]', '<input type="text" class="input_answer" v-on:click="handleInput()"/>') why have you used this? Commented Jul 20, 2022 at 16:52
  • 1. I have a string get from api Dan got 90 points for the Maths test, 70 points for the Science test, 80 points for the English test. The average point of the four tests Dan got was [answer] points. Jenny's family consumes 50 kg of rice in January, 55 kg in February and 45 kg on March. On average, Jenny's family consumes [answer] kg of rice a month. 2. Now, i have render in frontend. [link]drive.google.com/file/d/1AduuyKhGQ4oSga8PGMSZDbCPFH3uvGfr/…> -When user click Question 1 so forcus input 1.<br/> -When user click submit, will post all answers with api. Commented Jul 21, 2022 at 4:35
  • Will check it and try to help you Commented Jul 21, 2022 at 4:42

3 Answers 3

1

After spending an hour and so on this requirement, I came up with the solution.

Here you go (I added all the descriptive comments/steps in the below code snippet itself) :

// Template coming from API
var textFromAPI = "<p>Hello everyone! My name is [input]. I'm [input] year old</p>";

// getting the array of input tags. So that we can loop and create the proper input element.
const matched = textFromAPI.match(/(input)/g);

// Iterating over an array of matched substrings and creating a HTML element along with the required attributes and events.
matched.forEach((el, index) => {
    textFromAPI = textFromAPI.replace('[input]', `<input type="text" id="${index + 1}" v-model="inputValue[${index}]" v-on:keyup="getValue"/>`);
})

// Here, we are compiling the whole string so that it will behave in a Vue way.
var res = Vue.compile(textFromAPI)

var app = new Vue({
  el: '#app',
  data: {
    compiled: null,
    inputValue: []
  },
  render: res.render,
  staticRenderFns: res.staticRenderFns,
  mounted() {
    setTimeout(() => {
      this.compiled = res;
    })
  },
  methods: {
    getValue() {
      // Here you will get the updated values of the inputs.
      console.log(this.inputValue);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
</div>

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

Comments

0

Thanks @ Rohit Jíndal, but! When I use vue2 and it doesn't work. And there is an error:

TypeError: Cannot read properties of undefined (reading 'string')

I build this to a component and use anywhere in my project.

<render-html :text="question.quest_content" @handleAnswer="handleAnswer"></render-html>

enter image description here

Comments

0

I used and it's work.

this.$options.staticRenderFns = string.staticRenderFns;
return string.render.bind(this)(h)

Thanks so much!

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.