FSPR7
FSPR7
<div id="app">
<p v-uppercase>Hello, this text will be uppercased when clicked!</p>
</div>
<script src="app.js"></script>
</body>
</html>
File: app.js
Vue.directive('uppercase', {
bind(el) {
el.style.cursor = 'pointer';
el.addEventListener('click', () => {
const text = el.innerText;
el.innerText = text.toUpperCase();
});
}
});
new Vue({
el: '#app'
});
Output:
2CS201-Full Stack Web Development
<div id="app">
<ul v-list="items"></ul>
</div>
<script src="app.js"></script>
</body>
</html>
File: app.js
Vue.directive('list', {
bind(el, binding) {
const ul = document.createElement('ul');
binding.value.forEach(item => {
const li = document.createElement('li');
li.textContent = item;
ul.appendChild(li);
});
el.appendChild(ul);
}
});
new Vue({
el: '#app',
2CS201-Full Stack Web Development
data: {
items: ['Subject-1', 'Subject-2', 'Subject-3'] // Sample list of items
}
});
Output:
<div id="app">
<p v-format-date="date"> <!-- Apply the custom directive -->
<!-- The content will be manipulated by the custom directive -->
</p>
</div>
<script>
// Define the custom directive named 'format-date'
Vue.directive('format-date', {
// When the bound element is inserted into the DOM...
inserted: function(el, binding) {
// Get the date from the binding value
const date = binding.value;
</body>
</html>
Output: