How can I toggle designs using inputs?
<section id="assignment">
<!-- 1) Fetch the user input and use it as a CSS class -->
<!-- The entered class should be added to the below paragraph -->
<input type="text" v-on:input="setUser" />
<!-- (available classes: "user1", "user2") -->
<p :>
Style me!
</p>
<button>Toggle Paragraph</button>
<!-- 2) Use the "visible" and "hidden" classes to show/ hide the above paragraph -->
<!-- Clicking the button should toggle between the two options -->
<!-- 3) Add dynamic inline styling to the below paragraph and let the user enter a background-color -->
<input type="text" />
<p>Style me inline!</p>
</section>
I have already made user1 and user2 classes in css file, but when i try to output user as a class it is showing just user doesnot matter what i write in input field.
CodePudding user response:
Please take a look at following demo:
const app = Vue.createApp({
data() {
return {
user: null,
bgcolor: null,
toggle: false,
users: []
};
},
computed: {
classes() {
return this.users.map(u => 'user' u.id)
}
},
mounted() {
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(json => this.users = json)
}
})
app.mount('#demo')
.user1 {
color: red;
}
.user2 {
color: green;
}
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
<section id="assignment">
<select v-model="user">
<option v-for="(cls, i) in classes" :key="i">
{{ cls }}
</option>
</select>
<input type="text" v-model="user" />
<p :>
Style me!
</p>
<button @click="toggle = !toggle">Toggle Paragraph</button>
<input v-model="bgcolor" type="color" />
<p v-if="toggle" :style="`background-color: ${bgcolor};`">Style me inline!</p>
</section>
</div>