Insert Angular HTML tag into TypeScript

I am currently working on creating my own text editor, but I'm facing an issue. When I apply the bold style, all of the text becomes bold. How can I make it so that only the text I select becomes bold without affecting the rest of the text? Additionally, my current method does not add a tag, how can I achieve this as well?

Thank you in advance.

html

  <form [formGroup]="form">
    <div>
      <mat-form-field appearance="outline">
        <mat-label>textarea</mat-label>
        <textarea #text matInput formControlName="editor"></textarea>
      </mat-form-field>
    </div>
  </form>

ts.file

  @ViewChild('text') public textarea: ElementRef;

  public form: FormGroup;
  public isBold: boolean = false;

  constructor(private fb: FormBuilder, private renderer: Renderer2) {}

  ngOnInit(): void {
    this.createForm();
  }

  createForm() {
    this.form = this.fb.group({
      editor: null,
    });
  }

  addBoldStyle() {
    this.isBold = !this.isBold;
    if (this.isBold) {

      // let tag = document.createElement('strong');
      // let text = document.createTextNode(this.textarea.nativeElement.innerHTML);
      // tag.appendChild(text);

      this.textarea.nativeElement.style.fontWeight = 'bold';
    } else {
      this.textarea.nativeElement.style.fontWeight = null;
    }
  }

  add() {
    console.log(this.form.value);
  }

Answer №1

Experimenting with

declaring renderer as private: Renderer2
...
this.renderer.setStyle(this.el.nativeElement, 'background', 'yellow');
this.renderer.setProperty(this.el.nativeElement, 'innerHTML', '<p>Greetings Earthlings<p>');

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

What could be causing the issue of Vuejs 3.3 defineModel consistently returning undefined?

I am currently working with Nuxt version 3.5.1 and Vuejs version 3.3, however, I am encountering an issue where the defineModel macro always returns undefined. I am unsure why this is happening? <template> <input v-model="count"& ...

Using Typescript to deliver the parent component's props to its children prop

I have a goal to create a versatile component that can accept different props based on its usage in the project. The component should output its children prop along with all the given props (flow-through) and potentially some new constants calculated based ...

Steps to establish a maximum font size for my buttons

I've been working on creating buttons to decrease and increase the font size of my text. The functionality is there, but I want to establish a limit on how small or large the font can go. Here's what my current code looks like: <md-button ng ...

The lightbox feature seems to be malfunctioning, despite having successfully loaded jQuery

Struggling to activate lightbox on my gallery. Jquery is functioning as verified with an alert() in the documet.ready(). However, lightbox does not seem to be working properly. Only the links are operational. Below is a snippet of my code: <!DOCTYPE ht ...

JavaScript Age confirmation Overlay

I designed an age verification popup with the help of some online tutorials and a friend due to my limited knowledge of JavaScript. Check it out live here- My issue is that I want this popup to load/appear after the page content loads, but I'm not s ...

What is the best way to obtain a reference to an instance of my Angular 2 directive?

Angular 2 rc 5 was written using typescript 1.9 I am trying to access the instance of my attribute directive. Although I am using ViewChild, which typically works with components, it is giving me a handle to the element containing the directive. template ...

Stop the React timer count for a moment

I am currently attempting to implement a pause feature in a countdown timer using React. Here is the code snippet: export default function Home() { const timerRef = useRef(180) const [minute, setMinute] = useState(3) const [second, setSecond] = useS ...

Executing an on-click event on a button in PHP

I've been developing a learning site that involves teachers, students, and different subjects. To organize the registration process, I decided to separate the teacher and student registration pages since they input data into different tables in the da ...

Using the hover event in a jQuery plugin: A step-by-step guide

A star rating plugin I am developing is encountering an issue with implementing the hover event. jquery, (function($){ $.fn.extend({ rater: function(options) { var defaults = { } var options = $.exten ...

What causes the disappearance of CSS styles when attempting to modify the className in react js?

I am currently working on a basic react application, and I am trying to dynamically change the name of a div element using the following code snippet - <div className={'changeTab ' + (page.login ? 'leftSide' : 'rightSide')} ...

Utilizing a jQuery plugin or function with customizable options when adding elements dynamically

jQuery offers a useful feature where you can use $(document).on(...) to apply an event to newly added elements in the HTML, such as after an Ajax request. Can you provide an example of how to use this with a custom function/plugin like hoverpulse? Anothe ...

I'm encountering an issue with VUEJS components including my show route in their get call. How can I make my journals/:id pages function properly without encountering a Mime

I encountered a MIME type error stating: Refused to apply style from 'http://localhost:8080/journals/assets/css/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled. ...

Allow users to copy and paste on a website where this function is typically restricted

Just wanted to mention that I have very little coding knowledge, so I appreciate your patience. I'm attempting to paste something onto a site that doesn't allow it. Here is the link to the javascript they used to block it: A friend of mine recom ...

What is the protocol for displaying linear gradients that overlap?

My objective is to create a unique cutting corner effect using linear gradients. Achieving this on one corner is straightforward: body { background: #eac996; } .box { width: 100px; height: 100px; margin: 100px auto; padding: 20px; backgroun ...

Encountering a type error while trying to create a document within a nested collection in Firebase Firestore

While trying to submit the form with property data, I encountered an error message: FirebaseError: Expected type 'za', but it was: a custom Ha object. There doesn't seem to be any information available online explaining what a Ha object is o ...

Struggles encountered while configuring React

I'm in need of assistance with setting up React, even though I have both Node and npm installed. When I enter the following command: npx create-react-app new-test-react --use-npm I encounter the following error message: npm ERR! code ENOTFOUND npm E ...

Prevent users from deleting options in Autocomplete by disabling the backspace key

I am currently working on implementing the Autocomplete component from Material-Ui library to allow users to select multiple options, but I want to restrict them from directly removing any of those options. One issue I've encountered is that users ca ...

Navigating through an array and Directing the Path

My array contains objects as shown below: const studentDetails = [ {id:1, name:"Mike", stream:"Science", status:"active"}, {id:2, name:"Kelly", stream:"Commerce", status:"inactive"}, { ...

Setting a default value in react-select

Recently, I developed a react-select component that is compatible with redux-form. The SelectInput component looks like this: const MySelect = props => ( <Select {...props} value={props.input.value} onChange={value => props.input.on ...

Angular2 is throwing a Typescript Reference error because 'is not defined' in the context

I've been grappling with this specific error for the last couple of hours, and it's definitely not a run-of-the-mill undefined error. The issue arises when I define a value in the webpack plugins section (for a global variable) to serve as an API ...