How to apply bold styling to text with DOM Events

I am currently in the process of learning JavaScript and decided to practice by creating a text editor. The goal is to type something, click a button to change it to upper or lower case, bold, or italicize. While I successfully implemented the uppercase and lowercase functions, I'm facing an issue with the bold feature.

Below is the HTML code:

<h1 id="text">Write your text below</h1>

<div id="container">
    <input type="text" id="type">
</div>
<main>
    <ul>
        <li class="buttons">
            <button id="button1">A</button>
        </li>
        <li class="buttons">
            <button id="button2">a</button>
        </li>
        <li class="buttons">
            <button id="button3">B</button>
        </li>
        <li class="buttons">
            <button id="button4">I</button>
        </li>
    </ul>
</main>

And here is the associated script:

>  let text = document.getElementById("text")
>         let input = document.getElementById("type")
>         let button1 = document.getElementById("button1")
>         let button2 = document.getElementById("button2")
>         let button3 = document.getElementById("button3")
>         let button4 = document.getElementById("button4")
>         let value1 = document.getElementById("type").value
> 
> 
>         button1.onclick = uppercase
>         button2.onclick = lowercase
>         button3.onclick = bold
>         button4.onclick = italic
> 
>         function uppercase(){
>             text.innerHTML = value1.toUpperCase()
>         }
> 
>         function lowercase (){
>             text.innerText = input.value.toLowerCase()
>         }
> 
>         function bold(){
>             text.innerText = input.value.style.fontWeight="bold";
>         }

The title that will be modified is the h1 tag at the top of the HTML. In addition, I included the line:

let value = document.getElementById("type").value

to save time, but when I use button1 (uppercase), the text simply disappears. On the other hand, button2, with input.value, works perfectly fine. Can you explain why using var value1 causes the text to disappear and why I can't seem to make the text bold?

Appreciate any assistance on this matter!`

Answer №1

You seem to be encountering an issue where you are attempting to apply bold styling directly to the input.value. The correct approach would be to modify the style of the input element itself.

It's important to note that using input.value = is not a predefined function like toUpperCase; it pertains to styling instead.

One potential solution could involve utilizing input.value.style.fontWeight, but applying this change back upon clicking on another element can lead to messy code. An easier alternative would be to encapsulate the text in <b> tags, eliminating the need to reverse the adjustment.

Another point worth mentioning is the declaration of the input value within the function. By initializing it at the beginning, the value will remain empty as it is only set during page loading.

Answer №2

To apply CSS styling, you should target the object itself, not just the text content. Here's an example:

input.style.fontWeight="bold";

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

Unexpected behavior observed: Title attribute malfunctioning upon double clicking span element

I recently implemented the following code: <span title="hello">Hello</span> Under normal circumstances, the title attribute functions correctly when hovering over the element. However, after double clicking on the span element (causing the t ...

PHP - What is the best way to activate this array?

Seeking assistance with an array issue: I am trying to display random images with text, but the text is not appearing and when it does, the hyperlink is not positioned correctly under the image. Can anyone provide some guidance on this? <?php define( ...

Angular - ui-router states are not being detected

I'm currently working on a Spring and Angular JS web application project. The structure of the project is as follows:https://i.sstatic.net/xgB4o.png app.state.js (function() { 'use strict'; angular .module('ftnApp') .con ...

Creating an array of choices using jQuery: A step-by-step guide

In a jQuery solution I found on Stack Overflow, there was code used to show a specific div block only when certain menu options are selected. While I'm not well-versed in jQuery, I believe the $.viewMap block could be optimized to avoid repeating part ...

Executing JavaScript method when the enter button is pressed on an ASP.NET text box

I have implemented a bootstrap modal on my aspx page with a button that triggers an onclick event. Here is the code snippet: <asp:TextBox ID="LoginUsernameTextBox" runat="server" CssClass="login-name" placeholder="Use your email" /> <asp:TextBox ...

When the link_to element is clicked, use AJAX to replace the content of a specific

I was wondering about a modification to the link in view/users/show: <%= link_to "Photos (#{@user.photos.count})", user_path(@user), id: "photo_link", remote: true %> My goal is to dynamically change [1..6] to [1..-1] without having to rerender the ...

Implementing Ideone API functionality on Codeigniter with the use of ajax, javascript, and soapclient

I am new to using Codeigniter and I apologize if my question is basic. I found some code on this site: Working with IDE One API (Full project code available here) and I'm attempting to incorporate it into Codeigniter. I have been able to get it worki ...

Using AJAX and React to handle RESTful requests

Hello there, I am attempting to utilize a web service in React but am encountering issues with the AJAX function. I'm unsure if my code is working as expected. Here is a snippet of my code: prox= {"email":email, "password": password}; //tag comment $ ...

Ways to incorporate sass:math into your vue.config.js file

While using vue-cli with Vue 2.6 and sass 1.49, I encountered errors in the console related to simple division calculations: Deprecation Warning: Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0. I attempted to ...

CSS can only fade out without hiding afterwards

I'm struggling to create a CSS class and animation that will fade out or move a div along with its contents, resulting in the div having both display:none and visibility:hidden My attempts so far have been unsuccessful! I can either get the animation ...

Add a new row to the table when a dropdown option is selected, and remove the row when deleted. Ensure that the row is only added

Here is my specific requirement: I need a table with a default row containing a dropdown menu in the first column. When an option is selected from the dropdown, a new table row should be added with the same content as the main row and a delete button for ...

Send a function from a parent to its child component

I could use some assistance with JavaScript as it's not my strong suit. I'm currently trying to implement a solution from this answer on passing a function from a parent to a child in REACT, but I'm encountering some challenges. Would anyon ...

How do I utilize JSON to transmit a string to my server with Tornado?

Currently, I am facing an issue with my HTML input box and a JavaScript function that is triggered upon clicking a submit button. My goal is to send the data entered by the user in the input box to my Tornado server. Despite trying various options, I have ...

troubles encountered in implementing JavaScript to toggle the display of content upon clicking a link

Here is the code below. Upon page load, the form data will be hidden. When clicking on the comment link, it will toggle the visibility of the form content. Clicking on the comment link again will show the hidden content once more. What is desired is to ha ...

Is it possible to align a div on the same line with an h1 header that spans multiple lines using CSS?

I am currently working on the following code: <h1><span>Test Heading This is the text that I want to display on the right side. This is the text that I want to display on the right side. This is the text that I want</span></h1> < ...

When converting a .ts file to a .js file using the webpack command, lengthy comments are automatically appended at the end of

As a backend developer, I recently delved into UI technologies and experimented with converting TypeScript files (.ts) to JavaScript files (.js) using the webpack command. While the conversion works well, the generated .js file includes lengthy comments at ...

Ways to execute a function upon form submission

On my HTML page, I have a section dedicated to receiving a threshold value and calling a function with that value. <form id="distance_input" onSubmit="return false;" > <p>Enter a threshold</p> <div class="col-md-8" style=" ...

Removing an element from an array by evaluating each item within the array

Input array: ["temp/1/Lounge/empty", "temp/1/Lounge/66,66,66,66,66,66,66,66,64,64,64,64…,64,64,64,64,64,64,64", "temp/2/Lounge/empty", "temp/3/Lounge/empty"] I have a list of elements like the above. Each element consists of four parts separated by s ...

What could be the reason for the CORS failure when making requests from an HTTPS domain to an HTTP localhost

I have a secure rest service (implemented as an Azure Function), hosted on HTTPS, under domain A. My website is also running securely on HTTPS but on domain B. I've set the CORS to use a wildcard *. Using jQuery on my website, I send Ajax requests ...

Exploring ways to create a dynamic background image that allows the rest of the page to float over seamlessly

Currently working on a responsive website and almost done with the build. However, I came across a site where an image appears to be floating behind the rest of the webpage content. Tried searching for a solution using w3.css without any luck. Any sugge ...