I want to change a font weight from normal to bold by clicking checkbox input.
I tried to apply a ternary operator but it didn't work.
bold={ this.state.checkboxState ? this.props.bold : !this.props.bold }
What is the best way to implement this functionality?
Here is a codepen demo.
The code
class FontChooser extends React.Component {
constructor(props) {
super(props);
this.state = {
hidden: true,
checkboxState : true
}
}
toggle(e) {
this.setState({
checkboxState : !this.state.checkboxState
})
console.log(!this.state.isChecked)
}
render() {
return(
<div>
<input type="checkbox"
id="boldCheckbox"
checked={this.state.isChecked}
onClick={this.toggle.bind(this)}
/>
<span
id="textSpan"
bold={ this.state.checkboxState ? this.props.bold : !this.props.bold }
>
{this.props.text}
</span>
</div>
);
}
}
React.render(
<div>
<FontChooser text='Fun with React!' bold='false' />
</div>,
document.getElementById('container')
);
Thank you for your kind advice.
Use inline styles to bold specific text in React. js, e.g. <span style={{fontWeight: 'bold'}}>world</span> . The bold font will only be applied to the element to which it was added and its children. Copied!
Note that when using relative weights, only four font weights are considered — thin (100), normal (400), bold (700), and heavy (900).
✨ Using the Font link Go to https://fonts.google.com/. Here we are using the same font-family that linked in the above step. Last, you can add this style anywhere in your React component. const FontLink = () => { return( <div className="card"> <span className="font-link"> This is with Font Link.
You can apply the font-weight: normal as mentioned using the data attribute used by react-tooltip. Save this answer.
Change <span id="textSpan">
to the following:
<span
id="textSpan"
style={ this.state.checkboxState ? { fontWeight: 'normal' } : { fontWeight: 'bold' } }
>
{ this.props.text }
</span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With