What is the best way to print out multiple objects using console.log?

I am working on a JavaScript exercise where I want to create a function that produces an icon output using FontAwesome. While I have managed to display the icon and message, I am struggling to incorporate my custom styles for titles, emphasis, etc., using a function. I have attempted to use Switch, but it simply copies CSS as a string without applying it.

function generateIcon(ico_name, text, style ){ 
    switch(ico_name) { 
        case 'user':
            ico_name = '%c '
            font = 'font-family: "Font Awesome 5 Free"; font-weight: 900; font-size: 20px; content: "\f434";'
        break
    }  
    switch(style) { 
        case 'title':
            text = 'color: red'
        break
    }
    console.log(ico_name, font, text) 
}
generateIcon('user', 'player', 'title')

https://i.sstatic.net/cap6E.png

Answer №1

If you're unsure if this is the solution you need, consider utilizing document.getElementById() to access an element within the webpage and then implement CSS styles accordingly.

For example, if the element you want to adjust the font for has an id attribute set as "changeMyFont":

<span id="changeMyFont"></span>

You can modify its CSS properties using JavaScript like this:

var elem = document.getElementById("changeMyFont");
elem.style.fontFamily = "Font Awesome 5 Free";
elem.style.fontWeight = 900;
elem.style.fontSize = "20px";

To change the text color to red for a specific element, you can use the same approach. And when changing the content inside an HTML tag, utilize innerHTML (refrain from allowing user input here as it poses an XSS risk):

document.getElementById("elementToChange").innerHTML = "HTML goes here";

An HTML escape can be utilized in this scenario.

Answer №2

After several hours of trial and error, I managed to achieve my goal. The key was to combine 2 styles in a single console.log statement, although the code may appear convoluted and definitely needs improvement.

function icones(ico_nome,texto,style ){ // Captures the icon name, style to be used, and the text
    switch(ico_nome) { // Check argument icon
        case 'user':
            ico_nome = '%c ' // Icon will only be displayed after CSS declaration
            font_icon = 'font-family:"Font Awesome 5 Free"; font-weight: 900; font-size: 20px;' //Declare font for display
        break
    }  
    switch(style) { // Styling text
        case 'titulo': // Set styles for titles
            font_icon += 'color:red;' // Set Red color for Icon
            style_text = 'color:blue; font-family: Arial;' // Set styles for texts
        break
        default: 
            font_icon += 'color: black' // Set default color
    }
    console.log(ico_nome + ' ' + '%c' + texto,font_icon,style_text)  //Combine the 2 styles and display the output
}
icones('user','Usuario', 'titulo') //Applying values to function

Final outcome ---- View Console Output I still don't know how to directly insert the image into the post, sorry :)

Answer №3

One way to apply styling using Jquery is by utilizing the .css() function. Here's an example:

$("#element").css("font-size", "20pt")

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

How can I enhance Parallax scrolling performance on Chrome?

I recently completed a tutorial from the following link: After customizing it by changing the image to something different than clouds, I noticed that my version of the website was experiencing lag issues in Chrome but worked fine in Firefox. However, whe ...

Highcharts facing issues with data rendering

This is the JSON data I have: [ { "project_title":"sdsdsd", "project_ref_id":"112", "amount":"232323.00", "months":"Mar-2015" },{ "project_title":"test project 44", "project_ref_id":"113", "a ...

A variable that can be changed is available within a closure in Angular services

Currently, I am working with the Angular UI grid library and facing an issue while populating a custom dropdown filter. This filter is only meant to be applied to specific columns, which is why I have implemented a for loop to dynamically access these colu ...

Is it possible for Bootstrap to hide a grid column on extra small screens by setting its column width to zero?

Is there a specific Bootstrap class that allows me to hide a grid column for certain screen sizes without using media queries and the "display:none" style? ...

A guide to selecting the bookmark with a URL that is on a currently opened page

To gain a clearer understanding of my goal, follow these steps: Open the Chrome Browser and go to a URL, such as https://www.google.com. Once the page loads, locate and click on the bookmark labeled "ABC", which contains the URL ({window.open('/ ...

Utilizing HighCharts Bubble Graph with JSON Data Feed

I am currently facing an issue with my bubble chart series not plotting, even though I followed the HighCharts example tutorial. I am not seeing any errors on the browser console, which is making it challenging for me to troubleshoot. Here is the data I ...

Using Vue.js, learn how to target a specific clicked component and update its state accordingly

One of the challenges I'm facing is with a dropdown component that is used multiple times on a single page. Each dropdown contains various options, allowing users to select more than one option at a time. The issue arises when the page refreshes afte ...

What steps should I take to transform the chart data generation process during an AJAX callback?

I have created a code that generates a highchart chart, but now I want to convert the data used to create the chart into an AJAX Callback. This way, I can turn my chart into a live chart that updates every minute, and the only way to achieve this is thro ...

Attempting to cycle through rows of a table and triggering setInterval for each row does not appear to be successful

I am attempting to change the value of a cell from Offline to Online (and vice versa) within my DataTable by utilizing a web service. var $myTable = $("#my-table").dataTable(); var $myTableRows = $myTable.fnGetNodes(); for(var i=0; i<$myTableRows.leng ...

There is a lack of output coming from jasmine-node

Looking to expand my knowledge in JavaScript, Node.js, and jasmine, I recently started working on a test from "The Node Craftsman Book" called FilesizeWatcher. After setting up the package.json file and running "npm install" to add jasmine-node locally to ...

Managing CSS background images for various screen aspect ratios, from 16:9 to 9:16

Currently, I have a webpage that utilizes a background image set up like this: body { background-image : url("https://example.com/image.png"); background-size : cover; background-repeat : no-repeat; } While this s ...

Vue.js Google Places Autocomplete Plugin

I'm currently working on integrating Google Places Autocomplete with Vue.js. According to the API documentation, the Autocomplete class requires an inputField:HTMLInputElement as its first parameter, like shown in their example: autocomplete = new g ...

endless refreshing material ui accordion

Facing an issue with infinite rerender while trying to create a controlled accordion component using Material UI accordion. Here is the code snippet, any insights on why this could be causing an infinite rerender? const [expanded, setExpanded] = React.us ...

What is the best way to align grid elements vertically instead of horizontally on mobile devices using JQuery Mobile?

I've created a basic JQuery Mobile website with a grid containing four horizontally aligned blocks, each with text and a button. While it displays well on desktop browsers, the blocks appear cramped when viewed on my Android Galaxy phone in landscape ...

Sending a parameter from MVC 3 to JavaScript

Is there a way for me to successfully send the parameter counter to my BindUnbind() function? <div id="adver-list"> @{ var counter = 1; foreach (var adver in (IEnumerable<string>)ViewBag.AdverImages) { ...

What is the best way to include a maximum height in my ScrollToBottom element?

I am currently attempting to limit the maximum height of my component as adding user messages causes it to expand and stretch the whole page. However, I have been unsuccessful in setting a maxHeight property for the component. I am utilizing ScrollToBottom ...

Tips for placing images on top of each other without overlapping other sections using 'position: absolute' in HTML and CSS

Is there a way to position one image on top of another in a responsive manner? I tried using position: absolute and z-index, but the result is not responsive. When the screen size changes, the image ends up on top or behind another section instead of expan ...

Issues arise with the functionalities of FireFox related to transformations and transitions

I've been working on my CSS and here is what I have: img.buttonImg { -webkit-transition: all 0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; -o-transition: all 0.5s ease-in-out; -ms-transition: all 0.5s ease-in-out; transition: ...

In NextJs SSR, external components from a different package do not inherit styles when applied

I have incorporated an external react component into my Next.js app code. Here is a snippet of how the component appears: <AppBar className={clsx(classes.header)}> </AppBar> export const header = makeStyles((theme) => ({ header: { ...

Customizable form with dynamic content and interactive button within a set location

One distinct challenge not addressed in the similar inquiries below relates to a FORM component with both a variable segment and a fixed footer for submit buttons. The objective is to present: A header (a table set to 100% width including a logo and tex ...