Easy ways to manipulate the style of array components in Vue.js

Hey there all you programmers,

I'm reaching out to see if any of you have insight on how I can modify the style of a specific component within an Object array.

This is the code snippet:

<template>
  <div>
    <ul>
      <li v-for="jokes in joke" :key="jokes" :class="jokes">
        {{ jokes.title }}: <br />{{ jokes.text }} <br /><br />
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "Liste",
  props: ["joke"],
};
</script>

<style scoped>
.jokes {
  font-weight: bold;
}
</style>

Essentially, my aim is to only apply the bold styling to the jokes.title component of the jokes array, without affecting the entire content of the jokes array.

Thank you in advance for your assistance, greatly appreciated! <3

Answer №1

What do you think of this?

<template>
  <div>
    <ul>
      <li v-for="joke in jokes" :key="joke.id">
        <strong>{{ joke.title }}: </strong> <br />{{ joke.text }} <br /><br />
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "ListItems",
  props: ["jokes"],
};
</script>

Answer №2

While it is true that @thks173's answer does work, I would recommend avoiding the use of html tags for styling purposes. Here are a couple of additional points to consider:

  1. If you are using static classes, there is no need to bind the class attribute.
  2. The key attribute should be a unique primitive value such as the title.
  3. It is more convenient to loop over the jokes array and style each individual joke.

Here is an alternative solution:

<template>
  <div>
    <ul>
      <li v-for="joke in jokes" :key="joke.title" class="joke">
        <p class="title">{{ jokes.title }}</p>
        <p>{{ jokes.text }}</p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "Liste",
  props: ["joke"],
};
</script>

<style scoped>
 .joke {
   margin-bottom: 10px;
  }
 .joke .title {
  font-weight: bold;
  }
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS hover effects are malfunctioning

Having an issue with hover in CSS. Attempting to create a menu bar using saved PNG files, each file within its own div. When applying a class name and hover modifier, it only works on the first element. As the pointer moves to subsequent elements, they are ...

Navigate to a refreshed version of the page with varying parameters using React Navigation

Currently, I am utilizing React Navigation for navigating between different pages within my app. One of the pages is the Profile page which displays a user info card along with their posts. Within this Profile component, I have integrated the Post componen ...

Add the AJAX response to a div element when the submit button is pressed

Working on a WordPress project involving an AJAX call, I need to return the result of a row to a div with the ID #hero. The challenge is that since the result is inside a for loop and each row has its own hero div, I want the result to be returned to the c ...

Finding the height of a dynamic div in ReactJS when no CSS height is set

I'm encountering an issue when trying to determine the height of a dynamically created div that doesn't have a defined CSS height property. I'm working with React JS and have attempted to use jQuery, pure JavaScript, and React tools to acces ...

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

What is the reason why calling setState does not update the local state?

Hello everyone, I came across an intriguing React task and I'm struggling a bit with finding the solution. Task: Can you figure out why this code isn't working and fix it? Code: class BugFixer extends React.Component { constructor(props) { ...

Postpone the inclusion of html files within ng-include in AngularJS

I need to optimize the startup performance of my app by delaying the loading of a series of HTML files that are being included into my main controller. These files are not needed for at least 4 seconds while one specific HTML file processes. How can I de ...

Encountering a "Cannot modify" error in wp-admin while using inspect element

It seems like there is a slight error appearing in the Inspect Element Source Tab of Google Chrome (also checked in Firefox). I have searched extensively for a solution but haven't found anything helpful. My Wordpress theme displays wp-admin in inspec ...

Is there a way to replicate table cells in the style of Excel using jQuery?

Excel has a convenient feature that allows cells to be copied by dragging and dropping with the mouse. This same functionality is also available in Google Spreadsheets. I am trying to understand how Google has implemented this feature using JavaScript cod ...

Is there a copyright concern regarding CSS?

There are countless websites that spark my creativity. If I am inspired by a particular design, CSS, or JavaScript on one of these sites, am I allowed to copy it to my local folder and use it? For instance, let's say I come across a website called xy ...

Discovering how to adjust the "border-spacing" in a React Material-UI Table to ensure each row is nicely separated

I am looking to create Material-UI Collapsi Table Rows with separate styling. Check out the link for more information: https://material-ui.com/components/tables/#CollapsibleTable.tsx const theme = createMuiTheme({ overrides: { MuiTable: { root ...

What is the purpose of including an express server instance as an argument for the http module in Node.JS?

Currently delving into the realms of Node.JS, Express.JS, and Socket.IO. The tutorials I've come across so far showcase a complex series of code to kickstart each of these modules: var express = require("express"); var app = express(); var server = ...

Is there a way to extract information from an external XML document and incorporate it into an HTML file?

I am attempting to extract information from an XML file that is not stored on my website's server. My goal is to utilize this data for various purposes, such as generating charts. One specific example involves using weather data from an XML file locat ...

Reorganizing JSON structures by incorporating unique identifiers into nested elements

I am working on restructuring an incoming JSON object to utilize in a React component. The JSON data that I'm receiving is stored in jsonData. This is the current structure of my code: const jsonData = { "Jonas": { "position": "CTO", "em ...

Need assistance with PHP syntax?

Can you guide me on the correct syntax for creating a target link for a profile? <a href="index.php?p=profile&uid=<?php echo $_SESSION["uid"]; ?>"> <?php echo $_SESSION["username"]; ?></a> The variables I am working with are: ...

Challenges encountered when using node.js to write binary data

I've been attempting to write the binary body of a request to a file, but I'm encountering some issues. Although the file is successfully created on the server, I'm unable to open it and am receiving a 'Fatal error: Not a png' mess ...

Why does the browser keep converting my single quotation marks to double, causing issues with my JSON in the data attribute?

This task should be straightforward, but I seem to be facing a roadblock. I am trying to insert some JSON data into an input's data attribute, but the quotes in the first key are causing issues by prematurely closing the attribute. Here is the code ...

Error: Unable to parse JSON field value due to an unexpected OBJECT_START

Currently in my coding project, I am utilizing node and mongoose to update a Watson rank and access the database. My goal is to insert multiple documents into the collection. While I can successfully add a single document, I encounter issues when creating ...

Incorporating data retrieved through AJAX requests into a Python function

The Flask application described below continuously makes Ajax calls to a backend index function that queries a database and injects the results into a template. However, the injected data is not being refreshed after the Ajax calls are completed, leading ...

Change shapes of figure blocks using Internet Explorer (I am using IE11)

Good day! I have implemented this code to create a flip effect on the blocks of my website: .tegels { /*position:relative;*/ width:25.2%; height:37.4%; overflow:hidden; float:left; margin-top:3.5vw; margin-bottom:0px; m ...