Styling text in JavaScript and CSS with colored fonts and underlines

I am looking to customize the CSS for the font style.

<div id="a" onmouseover="chbg('red','b')" onmouseout="chbg('white','b')">This will change b element</div>
<div id="b">This is element b</div>

<div id="f" onmouseover="chbg('blue','g')" onmouseout="chbg('white','g')">This will change g element</div>
<div id="g">This is element g</div>

<div id="j" onmouseover="chbg('yellow','k')" onmouseout="chbg('white','k')">This will change k element</div>
<div id="k">This is element k</div>

From what I understand, this uses the 'chbg' function to modify background color.

If I want to add underline and change font color, how can I do this?

You can view a live example here: http://jsfiddle.net/NAuxn/

Answer №1

Make sure to include an additional parameter in your function call so that the style remains consistent when hovering out.

HTML

 <div id="a" onmouseover="chbg('red','b','underline','white')" onmouseout="chbg('white','b','none','black')">This will change b element</div>
 <div id="b">This is element b</div>

 <div id="f" onmouseover="chbg('blue','g','underline','white')" onmouseout="chbg('white','g','none','black')">This will change g element</div>
 <div id="g">This is element g</div>

 <div id="j" onmouseover="chbg('yellow','k','underline','white')" onmouseout="chbg('white','k','none','black')">This will change k element</div>
 <div id="k">This is element k</div>

JQUERY

function chbg(color, id, td, fc) { 
document.getElementById(id).style.backgroundColor = color;
document.getElementById(id).style.textDecoration = td;
document.getElementById(id).style.color = fc;
}

The code above uses four parameters like

chbg('red','b','underline','white')
. The third parameter indicates the text-decoration style and the fourth one specifies the color for mouse over.

When mousing out, the style is reverted back to normal. This approach effectively maintains consistency in styling. Consider creating a hover class and applying it on mouseover while removing it on mouseout for a more efficient solution.

DEMO

Answer №2

Utilize this function to customize the font color and apply an underline effect: http://jsfiddle.net/NAuxn/4/

function changeFontStyle(color, elementId) {     
  document.getElementById(elementId).style.color = color;
  document.getElementById(elementId).style.textDecoration = "underline";                        
}

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

Animation in CSS Appears Jumpy in Chrome

For some reason, the text inside a moving div (which is transitioning using CSS) seems to jitter and shake in Chrome. However, this issue does not occur in Firefox, where the movement is very smooth and achieves the desired effect. #hidexpert { display: b ...

Executing a C# function from an HTML hyperlink

I am in the process of developing a website using ASP.NET C#. The site will feature a GridView with data that can be exported, such as to Excel or other formats. To handle the export functionality, I plan to utilize code from this resource. When a user c ...

The Express.js server seems to be having trouble rendering a static JavaScript file

Currently, I am in the process of constructing a website and have implemented an express.js server to collect data submitted through a form. Prior to configuring the server, I had already developed the site using static js and css files. Once the connectio ...

Implementing a sibling combinator technique within Material UI's useStyles framework

Is there a way to change the background of one div when hovering over another using material ui? The traditional CSS method is: #a:hover ~ #b { background: #ccc; } Here is my attempt with Material UI: const useStyles = makeStyles(theme => ({ ...

Answer processing for the reminder dialog is underway

When I send a proactive message to a user, I want to initiate a 'reminder dialog'. The dialog is displayed, but when processing the response it goes back to the main dialog. This is how I currently set up my bot: const conversationState = new C ...

Angular-ui-bootstrap-tabs feature smooth transitions when switching between tabs, but lacks animation when moving

Just made some updates to an example by user @austin Encountering an issue when trying to transition backwards (from tab3 to tab2), but it works fine when transitioning forward. I'm not sure what I'm missing here, so any help would be appreciate ...

Having trouble connecting to Azure SQL server from my Node.js application

I'm attempting to run a basic query to my Azure SQL server from NodeJs, but I'm encountering errors indicating that I cannot establish a connection to the server. The details provided in my code are correct because I've used them successfull ...

Adjust parent div size based on image size increase

I am currently facing a situation where I have a page displaying an image, but sometimes it appears too small. In order to make the image larger, I have utilized CSS Transform and it is working well. However, the issue lies in the fact that the parent DIV ...

Are there any security risks in transmitting a password over HTTPS using jsonp?

Is it secure to send a password in JSONP using jquery over HTTPS for authentication if I can't use a JSON POST? EDIT: $.ajax({ type : "POST", url: "https://example.com/api.php", dataType: "jsonp", jsonp: "callback", data: { ...

Can you provide guidance on utilizing OneNote JavaScript APIs to interpret indented paragraphs within OneNote?

I keep a notebook that contains the following entries: https://i.stack.imgur.com/MLdO0.png For information on OneNote APIs, you can refer to this link (paragraph class selected already) https://learn.microsoft.com/en-us/javascript/api/onenote/onenote.p ...

Transfer the content from a specific div into another div

Within this code snippet, there are three buttons labeled P1, P2, and P3, each connected to a card div below them. On the right side, there is another div containing the text "TEXT SHOULD BE DISPLAYED HERE". The desired functionality is that clicking on an ...

How to create a stylish border line to separate rows using CSS

Having some trouble with HTML code... <table class="table table-condensed order-statistics"> <tr class="order-statistics-row"> <div class="row"> <div class="col-lg-4"> Order ID: </div> <div ...

Vue.js encountered an error: Unexpected TypeError in promise. The function $set is not recognized

Currently, I am working on fetching comments from the Reddit API and attempting to update an array using $set in order to refresh the view. However, I encountered an error: Uncaught (in promise) TypeError: $set is not a function Virtual Machine Component ...

Unable to reach the dropdown menu in CSS

Having trouble with creating a dropdown navigation bar? I've encountered an issue where the dropdown menu disappears as soon as I move my cursor off the buttons that reveal it. The menu itself displays properly, but accessing the drop down menu is pro ...

What is the process for updating information in Vue.js?

I need assistance with displaying the updated data in a modal. When I trigger the testing(data) function through a click event, the data appears correctly within the function. However, the template does not update and still shows the previous data. How can ...

Leveraging Bootstrap grid system within AngularJS elements

I am currently working on wrapping grid element divs into Angular components in order to streamline the input process and establish a standard: <bootstrap-row> <bootstrap-input-text col=6 ng-model="$ctrl.model" label="hey!"& ...

Adding metadata fields to an existing Markdown file within TinaCMS

Is it feasible to enhance a Markdown file using TinaCMS by introducing new frontmatter fields? Instead of generating a brand new Markdown file, my goal is to modify the current one by appending new frontmatter fields. Currently, I am able to modify a sin ...

Leveraging traditional code methods within AngularJs

With a multitude of older javascript functions for sign up/sign in operations through parse.com, I am considering integrating AngularJS for improved routing and other advantages. Is it feasible to establish an angular stack and encapsulate these functions ...

CSS: A guide to creating layouts

I wrote this code snippet to display a table: <div class="topologyBalloon" id="bl_c2f03b99-6d62-43c4-9a35-4b4cbf22a7db"> <a href="#close" class="closeTopologyBalloon">×</a> <div class="contentBody"> <table class="deta ...

My regular expression isn't functioning properly. Can someone please assist me in troubleshooting and resolving this issue?

Here is my code snippet: property_unit_plan.post('/bulkAdd',(req, res) =>{ Array.prototype.forEach.call(req.body, element => { db.sequelize.query('CALL sp_property_unit_plan_add_bulk( :unit_size_range, :no_of_bedrooms, :no_ ...