Issue with uppercase letters within a text input field

Imagine having an <input> and a <textarea> along with some JavaScript code that sends the value of the <input> to the <textarea> with additional text. How can you achieve this task?

The text-transform: capitalize property is set in the style of the <input> element. However, the text from the <input> does not get transferred to the <textarea> with proper capitalization. Applying text-transform: capitalize to the <textarea> makes all words capitalized, which is not desired. How can you work around this issue?

function sendText(){
  var input =document.getElementById("input");
  var textarea =document.getElementById("textarea");
  
  textarea.value ="My name is " + input.value;
  
}
input{
  margin-right:10px;
 float:left;
  text-transform: capitalize;
}
textarea{
  height:30px;
  width:140px;
  float:left;
  
}
<html>
<body>
  <input id="input" onkeyup="sendText()" placeholder="Your Name Here"><textarea id="textarea"></textarea>
</body>
</html>

Answer №1

When it comes to CSS, remember that it only styles the elements and won't change the actual content. You will have to handle value transformations yourself:

function updateText(){
  var input = document.getElementById("input");
  var textarea = document.getElementById("textarea");
  
  textarea.value = "Hello, my name is " + transformText(input.value);
}

function transformText(str) {
  return str.replace(/\b\w/g, function(char) { return char.toUpperCase(); });
}
input{margin-right:10px;float:left;text-transform:capitalize}textarea{height:30px;width:140px;float:left}
<input id="input" onkeyup="updateText()" placeholder="Enter Your Name"><textarea id="textarea"></textarea>

Answer №2

Would you like to give this a shot? I decided to make a simple change in the code by switching the textarea element to contenteditable for better styling. Additionally, I adjusted it to create a new element.

function updateText(){
  var input = document.getElementById("input");
  var editableContent = document.getElementById("editableContent");
    
  
  editableContent.innerHTML = "Hello, my name is <b id='name'>" + input.value + "</b>";
}
input{
  margin-right:10px;
  float:left;
}
#editableContent{
  height:30px;
  width:140px;
  float:left;
  border: 1px solid;
  font-family: Arial;
}
#name{
  text-transform: capitalize;
}
<html>
<body>
  <input id="input" onkeyup="updateText()" placeholder="Insert Your Name" />
  <div contenteditable="true" id="editableContent"></div>
</body>
<html>

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

Tips for centering a paragraph vertically within a div element

I'm struggling to achieve automatic vertical alignment for this. While I can manually center it using padding-bottom, I prefer a way to position it vertically on its own. How can I accomplish this while retaining the display: inline-block feature? &l ...

What is the best way to incorporate the "child_process" node module into an Angular application?

Trying to execute a shell script in my Angular application has been quite the challenge. I came across the "child process" node library that might be able to help me with this task. However, every attempt to import the library led me to the error message: ...

Encountered a SyntaxError in vue + webpack regarding an invalid range in character class

I am currently using webpack 4.29.3 and vue.js 2.6.3 to create a simple hello world project. I expected the index.html file to render correctly, but I encountered an error: SyntaxError: invalid range in character class. This error is confusing because I&ap ...

Align the text in the center of the button vertically

Struggling to vertically center text on a button? I understand the frustration! I've been working on this issue for a whole day with no success. My setup involves NEXT.js and TailwindCSS. <main> <div class='flex justify-center ite ...

Looping through an array using NgFor within an object

I've been working on pulling data from an API and displaying it on a webpage. Here is the JSON structure: {page: 1, results: Array(20), total_pages: 832, total_results: 16629} page: 1 results: Array(20) 0: adult: false backdrop_path: "/xDMIl84 ...

Is it possible to trigger a mouseover event on a background div that is obscured by a foreground tooltip in jQuery?

I created a unique effect where a background div fades in when moused over, followed by the foreground div fading in. However, I encountered an issue where the tooltip ends up "flashing" as the foreground steals focus from the background. For reference, h ...

PHP combined with jQTouch

I have a form on an HTML page that is passed to a PHP script. The PHP works fine, but I am facing some issues in displaying the HTML content on the PHP page. When I use <?php echo $message; ?> in the body of the HTML, the variable displays properly. ...

Ways to show just three buttons in a row using Bootstrap?

form{ display: flex; flex-flow: row wrap; justify-content: space-between; align-items: center; align-content: space-around; } form .buttons{ width: 100%; border: none; background-color: #ffffff00; cursor: pointer; } form .buttons img{ widt ...

Aligning Divs in a Responsive Browser

In the following illustration, I am facing an issue with my responsive code. I have three sections named square1, 2, and 3, each containing a white div with text on top and an icon positioned absolutely. Everything works fine when the browser width is 920p ...

Challenges with Angular 4 service initialization

Having trouble with my authentication service. The constructor is being called 259 times when making an HTTP request, but only once when the call is removed. I am using a shared module to provide a unique instance of the service. Angular version: 4.4.4 C ...

IBM Watson Conversation and Angular Integration

After recently diving into Angular, I am eager to incorporate a Watson conversation bot into my angular module. Unfortunately, I'm facing an issue with including a library in Angular. To retrieve the Watson answer, I am utilizing botkit-middleware-wat ...

CSS - Utilize floating divs to dynamically populate empty spaces

I am currently working on creating a grid layout on my website, . Despite using multiple divs (images) with the float: left; property, I am noticing empty spaces and gaps that are not being filled by the floating divs. Thank you in advance for any assist ...

Refresh database records

I am currently using grails and I am exploring ways to update my database table utility by selecting values from two drop down menus. These drop down menus are populated with values from the database based on user selections from another drop down menu. My ...

Utilizing various if conditions in conjunction with the jQuery chart plugin Piety

Check out this Fiddle example I'm interested in experimenting with different color combinations using a small chart plugin called piety. The documentation demonstrates how to set different colors for a pie chart: $(".bar-colours-1").peity("bar", { ...

Adjust the size of child divs in relation to their parent divs using jQuery

I am working with 4 divs and trying to adjust the size of the inner divs in relation to the parent divs dynamically. I have added a .top class, but I'm unsure if it is necessary or beneficial. Here is my fiddle for testing purposes: http://jsfiddle.n ...

Working with Node.js and JavaScript's Date object to retrieve the time prior to a certain number of hours

I am currently working on a script in node.js that is able to locate all files within a specific directory and retrieves their modified time: fs.stat(path, function(err, states){ console.log(states.mtime) }) After running the script, it ...

Using jQuery .css({}) is not causing negative margin to function as expected

$('#thankYouMessage').css({"height": textHeight, "margin-top:": "-52px", "padding-left": "19px"}); The CSS property 'padding-left:' will be applied as expected, but the negative margin will not take effect. The 'margin-top:' ...

The Radio button and Checkbox values are causing an issue where the Radio button is continuously summing upon clicking without a limit. Why is this happening? Check out

I'm currently stuck in a function and believe it would be helpful for you to guide me along the correct path. My goal is to sum the values of checked boxes with the values of checked radio buttons. However, the radio values are continuously accumulat ...

creating a form layout with inline buttons

I have encountered an issue with aligning 2 buttons and a form with input type in a single line. Even though I have tried different approaches, I am unable to achieve the desired inline layout. Here is my current setup: .btn-registerLayout { backgr ...

Dragging and dropping an HTML canvas element causes its width and height to be reset to zero

I am currently developing a customizable dashboard that allows users to rearrange dashboard tiles (represented by div elements) and position them anywhere within the dashboard. HTML Structure The HTML structure is outlined in the snippet below: <div cl ...