Javascript div font color

Css div element font color javascript

The overarching question: Any suggestions on a JavaScript function that sets the text color on all elements within a div? Solution 1: Note child elements color shouldnt have colors, its taken from parent div color Solution 1: CSS: ​Not all browsers accept words as colors try using HEX-code or rgb().

Setting text color on all elements within div

I have a SCSS-mixin that looks like this:

/** * Text color */ @mixin text-color( $color ) < color: $color; h1, h2, h3, h4, h5 < color: $color >p, li < color: $color; >a < color: $color; text-decoration: underline; &:hover < text-decoration: none; >> > 

It’s not the nicest of things, but it’s really handy. I use it, whenever the text color of some child-element is playing up. Every now and then, there is an li or an a that has some styling from further up the tree.

Читайте также:  Charat function in javascript

I’m looking for the JavaScript equivalent. The reason is, that I would like the user to select a hex-color, whereafter all elements will get that color.

My current version, is to do something like this (in a Vue-mixin):

mounted()< let chosenHex = #FF0000; this.setTextColorOnAllElements( chosenHex ); >, methods: < setTextColorOnAllElements( textColor )< setTimeout( () =>< // Otherwise the el's aren't rendered this.setTextColorOnTagType( textColor, 'h1' ); this.setTextColorOnTagType( textColor, 'h2' ); this.setTextColorOnTagType( textColor, 'h3' ); this.setTextColorOnTagType( textColor, 'h4' ); this.setTextColorOnTagType( textColor, 'h5' ); this.setTextColorOnTagType( textColor, 'h6' ); this.setTextColorOnTagType( textColor, 'h7' ); this.setTextColorOnTagType( textColor, 'p' ); this.setTextColorOnTagType( textColor, 'li' ); this.setTextColorOnTagType( textColor, 'span' ); this.setTextColorOnTagType( textColor, 'a' ); >); >, setTextColorOnTagType( textColor, tagType ) < if( this.id )< let elements = document.querySelectorAll( '#' + this.id + ' ' + tagType ); elements.forEach( (element) =>< element.style.color = textColor; >); > > > 

It’s just long-winded and feels clumbsy.

The overarching question: Any suggestions on a JavaScript function that sets the text color on all elements within a div?

Note child elements color shouldnt have colors, its taken from parent div color

const container = document.querySelector('.container'); const colorClassPicker = document.getElementById('colorClassPicker'); const colorHexPicker = document.getElementById('colorHexPicker'); const addClass = (className) => < container.classList.replace(container.classList.value, className); >; const addStyle = (hex) => < container.style.color = hex; >; // colorClassPicker.addEventListener('change', (e) => < // addClass(e.target.value); // >); colorHexPicker.addEventListener('change', (e) => < addStyle(e.target.value); >); // addClass(colorClassPicker.value); addStyle(colorHexPicker.value);

Also you could do it by toggle classes in parent div

// colorClassPicker.addEventListener('change', (e) => < // addClass(e.target.value); // >); 

Are you looking for something like this? Run the snippet.

//use querySelectorAll to get all elements within the div var children = document.querySelectorAll("div p") //loop through those elements and change the color for (var i = 0; i
Lorem

Ipsum

Lorem

Ipsum

Lorem

Ipsum

Lorem

Ipsum

Alternatively, target the parent div to make it even easier:

//use querySelector to target parent div var parent = document.querySelector("div"); //set color on parent div to change color of children parent.style.color = "blue";

Probably not what you’re looking for, but this technically works by virtue of creating and inserting a stylesheet.

const forceColor = (selector, color) => < const rule = [ `$< color: $; >`, ['h1', 'h2', 'h3', 'h4', 'h5', 'p', 'li'].map( v => `$ $` ).join(', ') + ` < color: $; >`, `$ a < color: $; text-decoration: underline; >`, `$ a:hover < text-decoration: none; >`, ].join(' '); const css = document.createElement('style'); css.type = 'text/css'; css.styleSheet.cssText = rule; document.querySelector('head').appendChild(css); >; 
forceColor('.someCSSSelector', '#FF0000'); 

Simple oneliner Javascript:

document.querySelectorAll("p"). forEach(function(x)< x.style='color:brown;background:#CCC';>);

Head 1

Paragraph 1


Head 2

Paragraph 2

Array.prototype.slice.call(document.getElementsByTagName('p')). forEach(function(x)< x.style.color='brown';x.style.background='#CCC';>); 

Change menu text color when div element touches menu, Have a look at IntersectionObservers, and create as many of them as you have white divs. if your site needs to support IE11, you’ll need to

Toggle text color of all div elements

Is there a way to change all the text color to black when the checkobx is checked and back to their original color when unchecked?

I tried the using toggle but am not sure how to retain the original color. The current code is below and here is a fiddle.

 var colors = ['red', 'blue', 'purple', 'green']; $(document).ready(function () < $('.ws-css-table-td').each(function () < var theColor = colors[Math.floor(Math.random() * colors.length)]; $(this).css('color', theColor); >); >); 
.ws-css-table < display: table; >.ws-css-table-tr < display: table-row; >.ws-css-table-td
 var colors = ['red', 'blue', 'purple', 'green']; $(document).ready(function() < $('.ws-css-table-td').each(function() < var theColor = colors[Math.floor(Math.random() * colors.length)]; $(this).css('color', theColor); >); >); $('#blacktext').click(function() < $('.ws-css-table-td').toggleClass('blackcolor'); >)
.ws-css-table < display: table; >.ws-css-table-tr < display: table-row; >.ws-css-table-td < display: table-cell; border: 1px solid #000; width: 15px; height: 15px; text-align: center; vertical-align: middle; >.blackcolor

You can do this using a onclick, add to the checkbox:

Now we are gonna make a function in javascript called makeBlack:

How to set the text-color for different elements in CSS, Colors are very important to give a good look to the website and engage the users. So, different colored text elements are added to enhance

Change the font color of text inside div tag on hovering

I am trying to change the color of the text and underline it, when a user hovers over a text.

I tried the following and it doesn’t work. I did look for a solution all over the internet and I didn’t find any that suited my particular need.

 .container < width: 100px; float: left; background: #e2edf9; overflow: hidden; >.content .content:hover 
**EWR**
**NRT**
.container < width: 100px; float: left; background: #e2edf9; overflow: hidden; >.content .content:hover

​Not all browsers accept words as colors try using HEX-code or rgb().

In IE there must be declared a for the :hover selector to work on other elements than the element.

I’m not so sure about :hover support on elements other than link in

If push comes to shove there is always your javascript whip:

 .container < width: 100px; float: left; background: #e2edf9; overflow: hidden; >.content .content:hover 
**EWR**
**NRT**

This fiddle works for me in IE8, see fiddle here, except when running in quirks mode, do you have quirks mode on?

Is javascript an option, seems like the best solution for you.

Change font color of nested HTML element with JavaScript, If you would like to only highlight the s which are direct children of a .speed element, you can change the selector string to .speed > a .

Источник

How to Change Font Color of Div in JavaScript?

To change the font color of a div using JavaScript, get reference to the element, and assign required color value to the element.style.color property.

Conclusion

In this JavaScript Tutorial, we learned how to change the font color of a div using JavaScript.

  • How to Change Border Color of Div in JavaScript?
  • How to Change Border Radius of Div in JavaScript?
  • How to Change Border Style of Div in JavaScript?
  • How to Change Border Width of Div in JavaScript?
  • How to Change Bottom Border of Div in JavaScript?
  • How to Change Font Family of Div in JavaScript?
  • How to Change Font Size of Div in JavaScript?
  • How to Change Font Weight of Div in JavaScript?
  • How to Change Height of Div in JavaScript?
  • How to Change Left Border of Div in JavaScript?
  • How to Change Margin of Div in JavaScript?
  • How to Change Opacity of Div in JavaScript?
  • How to Change Padding of Div in JavaScript?
  • How to Change Right Border of Div in JavaScript?
  • How to Change Text in Div to Bold in JavaScript?
  • How to Change Text in Div to Italic in JavaScript?
  • How to Change Top Border of Div in JavaScript?
  • How to Change Width of Div in JavaScript?
  • How to Change the Background Color of Div in JavaScript?
  • How to Change the Border of Div in JavaScript?
  • How to Clear Inline Style of Div in JavaScript?
  • How to Hide Div in JavaScript?
  • How to Insert Element in Document after Specific Div Element using JavaScript?
  • How to Underline Text in Div in JavaScript?
  • How to get Attributes of Div Element in JavaScript?

Salesforce

SAP

Accounts

Database

Programming

App Developement

Mac/iOS

Apache

Web Development

Online Tools

©Copyright — TutorialKart 2023

Источник

How to change the font color of a text using JavaScript?

This tutorial teaches us to change the font color of the text using JavaScript. While working with JavaScript and developing the frontend of the application, it needs to change the font color of the text using JavaScript when an event occurs.

For example, we have an application which can turn on or off the device. We have a button to turn on or off the device. When the device is on, make the button text green. Otherwise, make the button text red.

So, in such cases, programmers need to change the font color using JavaScript. We have some different method to overcome the problem at below.

Access the Element and Change the Style

In this section, we will access the element by id or class name using JavaScript. Users can change the style of the element using the .style property. Also, we can change the specific style, such as element color, background color, size, etc.

In our case, we will change the color attribute values to change the font color. Users can follow the below syntax to change the font color using JavaScript.

Syntax

let element = document.getElementById(' element_id '); element.style.color = colorCode;

Parameters

  • colorName − It is the new color, which users want to apply to the text. It can be color name, color’s hexadecimal code, or RGB values.

Example

In the below example, we have created a button. When the user clicks on that button, we will call a function named changeFontColor(). Inside the function, we are accessing the button element using its id and changing the color using the color attribute of its style

html> head> style> button height: 30px; width: 100px; > /style> /head> body> h2> Change the font color using JavaScript./h2> div id = "fonts"> Click the button to change the color of font inside the button./div> button onclick = "changeFontColor()" id = "btn" >change color/button> script> // function to change the font color of button function changeFontColor() let button = document.getElementById("btn"); // access the button by id let color = button.style.color; if (color == "red") // if button color is red change it green otherwise change it to red. button.style.color = 'green'; > else button.style.color = 'red'; > > /script> /body> /html>

When users will click on the ‘change color’ button, it will toggle the button color between green and red.

Change the color of all text

In this section, we will learn to change the color of all body text. You can consider the scenario. When we apply the dark or light theme to the application, it is not good practice to change the color of every element one by one. Rather, we can change the color of all body text with just a single click.

Users can follow the syntax below to change the body text’s font color.

Syntax

document.body.style.color = color

Example

In the below example, we will change the color of the all body text, instead of changing the text of the particular element.

html> head> style> button height: 30px; width: 100px; > body color: blue; > /style> /head> body> h2> Change the font color using JavaScript./h2> div id = "fonts"> Click the button to change the color of font of the whole body/div> button onclick = "changeFontColor()" id = "btn">change color/button> script> // function to change the font color of button function changeFontColor() // toggle the body text color on button click. let color = document.body.style.color; if (color == "blue") document.body.style.color = 'pink'; > else document.body.style.color = 'blue'; > > /script> /body> /html>

When user clicks on the button, it will change the color of all text between pink and blue.

Use the String fontcolor() Method

In this section, we will learn about the fontcolor() method. We can apply the fontcolor() method on any text string, and it returns the HTML element with the color attribute.

Users can follow the below syntax to use the fontcolor() method.

Syntax

let text = "some text"; text.fontcolor("color");

Parameters

Return values

Example

In the below example, we will change the color of a particular string using String fontcolor() method.

html> head> style> button height: 30px; width: 100px; > /style> /head> body> h2> Change the font color using JavaScript./h2> div> Click the button to change the color of below text using i> fontcolor() /i> method./div> div id="fonts"> hello world!/div> button onclick = "changeFontColor()" id = "btn">change color/button> script> // function to change the font color of button function changeFontColor() let fontsDiv = document.getElementById("fonts"); let string = "hello world"; fontsDiv.innerHTML = string.fontcolor("green"); > /script> /body> /html>

In the output, users can observe that when they clicks the button, font of the “hello world” changes to the green.

In this tutorial, we have learned to change the all text of the body with a single click. Also, we learned to change the color of the text of a single element using the style attribute of the element. In the last section, we have learned about the fontcolor() method which is deprecated so it is not recommended to use.

Источник

Оцените статью