Use JavaScript to convert only the initial letter to uppercase

Once again, I am in the process of learning! Apologies for the simple questions, but learning is key...

Attempting to implement a trick I found online to change all letters to uppercase, I am now trying to adjust it to only capitalize the first letters. Take a look at the code snippet below - specifically focusing on the JavaScript onkeyup:

Code snippet that works:

<label for="class">Classification</label>
<input type="text" name="class" id="class" value="" required="true" placeholder="" style="text-transform:uppercase" onkeyup="javascript:this.value=this.value.toUpperCase();" />

Code snippet that does not work:

<label for="judge">Judge's Name</label>
<input type="text" name="judge" id="judge" value="" required="true" placeholder="" list="judges" style="text-transform:capitalize" onkeyup="javascript:this.value=this.charAt(0).toUpperCase() + this.substr(1);" />

Answer №1

You missed retrieving the value property. To apply charAt and substr, you should do it on a string rather than directly on the DOM element.

<label for="judge">Judge's Name</label>
<input type="text" name="judge" id="judge" value="" required="true" placeholder="" list="judges" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" />


Just so you know: It's always better to utilize CSS, as there is an option for capitalizing the first letter using text-transform:capitalize. Instead of inline script code, consider using a function to enhance readability and manageability.


UPDATE: Another recommendation is to use the HTML5 input event instead of keyup for better functionality.

<label for="judge">Judge's Name</label>
<input type="text" name="judge" id="judge" value="" required="true" placeholder="" list="judges" style="text-transform:capitalize" oninput="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" />


You can also avoid the issue of the moving arrow key (as pointed out by @stdob) by adding an additional hidden input field named judge and updating the value in that field to transfer to the server.

function transform(ele) {
  var hid = document.getElementById('judge');
  hid.value =
    ele.value.charAt(0).toUpperCase() + ele.value.substr(1);
}
<label for="judge">Judge's Name</label>
<input type="text" name="judge" id="judge" value="" required="true" placeholder="" list="judges" style="text-transform:capitalize" onkeyup="transform(this)" />
<input type="hidden" name="judge" id="judge" value="" />

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

Creating responsive D3.js charts for various screen sizes and devices

After creating a chart using d3.js, I noticed that the chart does not resize when zooming in or out on the web browser window. Below is the code snippet of what I have attempted so far: <!DOCTYPE html> <html> < ...

Adjust the color of a contenteditable div once the value matches

I currently have a table with some contenteditable divs: <div contenteditable="true" class="change"> This particular JavaScript code is responsible for changing the color of these divs based on their content when the page loads. However, I am now ...

Having trouble deleting JavaScript object properties within a loop?

Struggling to comprehend the behavior of this particular piece of javascript code. const devices = searchResult.results.forEach(device => { const temp = Object.keys(device.fields); for(var property in temp) { if(device.fields.hasOwnPro ...

The index "is_ajax" has not been defined

I attempted to create a simple login functionality using Ajax. Following a tutorial that didn't use classes and functions in PHP, I tried restructuring the code with classes and functions. However, I encountered the error: Undefined index: is_ajax He ...

Modify the contents of an array within a string using JavaScript

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com"; let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"]; I'm trying to replace all URLs in the 'message' array with "***" from the 'ur ...

What is the reason for the presence of additional space below when using x-overflow:hidden?

When I place two spans inside each other and use overflow-x:hidden on the inner span, it results in extra space below the inner span. Why does this happen? <span style="" class="yavbc-color-tip"><span style="">Some text</span></span&g ...

I am having trouble getting the (:active) pseudo-class to function properly with the menu on my WordPress theme

Purchased a theme called HelpGuru and encountering issues with the CSS of the menu. Reached out to support but they were unable to assist, directing me to a company that specializes in customizing WordPress themes instead. The link to the demo can be found ...

Parsing HTML with a simple DOM parser

I am utilizing the simple php dom parser in my project. Within my loaded dom, there is a link that appears like this in html: <a href="...">Some const tesxt</a> I am wondering how I can utilize the find function to select this particular obje ...

Configure your restify REST API server to handle both HTTPS and HTTP protocols

I'm currently utilizing node.js restify version 4.0.3 The code snippet below functions as a basic REST API server supporting HTTP requests. An example of an API call is var restify = require('restify'); var server = restify.createServer( ...

What could be causing the issue with Vite build and npm serve not functioning together?

After shifting from CRA to VITE, I am encountering a problem with serving my app. I successfully build my app using vite build. and can serve it using Vite serve without any issues. However, I want to use npm's serve command. Whenever I run vite bui ...

Automatically update Dropdown options with value and text through an ajax json request

I am working on dynamically populating a dropdown list with text and values fetched using an ajax call to retrieve a JSONObject. While my current code successfully populates the dropdown, I need the value to be different from the text as I intend to store ...

Is there a way to retrieve the information from the v-text-field without using the v-model directive?

<div v-for="i in parseInt(questionCount)" :key="i"> <v-layout> <v-flex xs6 offset-3 mt-15" > <label for=""> Enter question number {{index}}:</label> <v-text-fi ...

Ways to retrieve a value from a span using the Document Object Model

sample.html <span id='char'>{{value}}</span> sample.ts console.log((<HTMLInputElement>document.getElementById('char'))) This code snippet will display <span id='char'>ThisIsTheValueupdated</sp ...

When attempting to call a Firebase Cloud Function URL in an AngularJS $http request, an Access Control Origin Error

I recently created a cloud function that involves linking with Plaid. I'm currently working on calling this function using AngularJS's $http method. While the cloud function code is being executed, I encountered an error in my console instead of ...

JS form validation malfunctioning

XHTML <form name="suggestion" method="post" action="suggestion.php" class="elegant-aero" onSubmit="return validate()" > <label> <span>Message :</span> <textarea id="Message" name="m ...

Implementing a Jquery check based on a checkbox

Hey, I'm having an issue with a condition. When I uncheck the checkbox, it doesn't uncheck. I've tried to make a block display, but the JavaScript isn't working. I attempted to add: document.getElementById("Reload").style.display = "b ...

What is the best way to generate a box when a button is pushed?

How can I make a button create a box with text inside it when clicked? <div class="trades"> </div> <button onclick="create()">add</button> Here is the JavaScript function to create the box: function create() { var box = docu ...

Tips on choosing one specific JSON object from a group and exploring its structure in Python

Looking for guidance to access a specific javascript element named SOURCE.pdp.propertyJSON and extract its attributes in a Pythonic way from a webpage filled with different script elements. Browse through the HTML source code below to get an idea and then ...

Language setting required for dijit.form.DateTextBox

I am attempting to create a calendar control dynamically using Dojo 1.5 and set the language to Spanish, but so far I have been unsuccessful. I tried using "lang:es-es" among other things with no success... here is the element: <input type="text" const ...

Using conditional rendering to compare the outcome of an asynchronous function within v-if condition

I have a Vue component for a sidebar with the following template: Vue.component('navigation',{ template:` <ul class="nav"> <li v-if="checkIfUserIsAdmin() == true" id="adminMenu"></li> <li id="userMenu">& ...