How to Use JQuery to Capitalize the First Letter of Every Word in a Sentence

How can I capitalize the first letter of each word in a text field using JQuery? I've been struggling to achieve this.

Here is the Bootstrap code that I'm currently using to extract data from a text field when a button is submitted

<button class="btn btn-primary" type="button" id="btn4" value="alternatingCase">Alternating Case</button>

The following JQuery code only converts the first letter of the paragraph to uppercase:

$("#btn4").click(function(){
  var input = $("#input1");
  input.val(input.val().charAt(0).toUpperCase() + input.val().substr(1).toLowerCase());
  });

Answer №1

You appear to be interested in capitalizing each word.

$("#btn4").click(function() {
  // create an array by splitting the input value
  let words = $("#input1").val().split(' ').map((word) => {
   // capitalize the first letter of each word and convert the rest to lowercase
   return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()
  })
   // join the words back together to form a string
  $("#input1").val(words.join(' '))
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="input1">
<button class="btn btn-primary" type="button" id="btn4" value="alternatingCase">Alternating Case</button>

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

incapable of modifying the contents of an array

Struggling with a JavaScript issue (not angular related, but within an Angular context) for hours without any success. I've condensed the content into shorter paragraphs and reassigned the subcontent back to the array's content property, but the ...

Is it possible to use two mergeMap operators concurrently in RxJs?

Recently, I started using RxJs alongside redux and have successfully created a working stream. Below is the code snippet of my action pipe: action$.pipe( ofType(DELETE_CALL_HISTORY), withLatestFrom(state$), mergeMap(() = ...

Transform List Elements into Interactive jQuery Toggles with Dynamic Functionality

I want to create a feature that automatically converts list items into jQuery toggles for elements on a webpage. For instance, consider the following code snippet: <div id="page"> <div id="menu"> <ul> <li clas ...

I'm encountering an error in my routes index.js file that says "Module not found"

The issue is stating "Cannot find module './routes/index'" despite it being located in that directory (even when static is set to that folder) Here is the error: root@ip*censored*:/home/ubuntu/*censored*# module.js:471 throw err; ^ Err ...

Is there a way to switch my image buttons to a different image when clicked?

In order to create interactive buttons for a website, I designed four unique .PNG images using Photoshop. Two of these images are intended to serve as default buttons, while the other two are meant to be used as "post-click" buttons. For the default butto ...

Locate a piece of text with jQuery and enclose it within a specified container

Check out this code <form method="get" name="form_delivery"> Pick the country where you want your delivery<br> <select name="deliverymethod"> <option value="0" selected="selected">Choose a country / region</option> ...

The functionality of event bubbling appears to be ineffective while utilizing a bootstrap modal in an AngularJS application

I have a question regarding the use of Bootstrap modal. To begin with, I apologize for any issues in understanding my question due to my English skills. I have created a button as a directive to dynamically add, with reference to the following links. . ...

Is there a way to simultaneously view and send this JSON data to the server using console.log?

I'm looking to inspect the JSON data being sent and received by the server, but I'm struggling to understand how promises work in this scenario. When I use console.log() on the function body, I see Promise { pending }. Unfortunately, I can' ...

Substitute regular expressions with several occurrences by their respective capture groups

I am attempting to use JavaScript to extract only the link text from a string and remove the href tags. The expected behavior is as shown below: <a href='www.google.com'>google</a>, <a href='www.bing.com'>bing</a> ...

Scanning barcode and Qrcode with Angular js HTML5 for seamless integration

Looking to scan Barcode and Qrcode on Android, iPhone, and iPad devices for a project that is built on AngularJS and HTML5 as a mobile website. The requirement is not to download any third-party native application on the device, ruling out the use of nati ...

having difficulty choosing a particular identifier from a JSON string

I'm currently working on a project to create an admin page for managing client information. However, I've encountered an issue where I am unable to select the client's unique ID to display all of their information on a separate page. On the ...

An empty image placeholder was placed on the page using jQuery

I am having an issue with displaying an image in a div tag on my website. <div class="imsSummaryItem" id="imagesPreview"> </div> <asp:Image ImageUrl="C:\Users\John\Desktop\TempFolder\16.jpg" runat="server" /> --n ...

Switch between different classes with JavaScript

Here is the code that I am working with: This is the HTML code: <div class="normal"> <p>This is Paragraph 1</p> <p>This is Paragraph 2</p> <p>This is Paragraph 3</p> <p>This is Paragraph 4&l ...

The art of replacing material-ui styles with styled components

As a newcomer to UI material design, I am eager to create my own customized Button Component using styled-components. I am facing a challenge in overriding the CSS based on different button variations such as "primary" or "secondary". You can find my cod ...

Merging arrays with the power of ES6 spread operator in Typescript

My goal is to merge two arrays into one using the spread object method as shown in the code snippet below: const queryVariable = { ...this.state, filters: [...Object.keys(extraFilters || {}), ...this.state.filters], } The this.state.filte ...

Creating a unique custom theme for Twitter Bootstrap at 1200px and 980px screen sizes

Using the Twitter Bootstrap CSS framework, I have successfully created a custom navigation bar for desktop devices with a width of 1200 pixels and above. Now, I want to create similar navigation bars for other screen widths such as 980px, tablets, and phon ...

Choose all checkboxes across the entire webpage

Given the code below: <input type="checkbox" name="categories[9507]"> Is there a way to write a JavaScript command that can automatically select all checkboxes with similar naming structures on the entire page? The only difference in the names is t ...

Manipulating the DOM within an Angular application

What is the best way to perform DOM manipulation in Angular without using jQuery? Here is an example of code using jQuery: $(".next-step").click(function (e) { var $active = $('.wizard .nav-tabs li.active'); $active.next().removeClass(& ...

Transmitting JSON information using post method through .ajax(), as well as receiving a JSON reply

Seeking assistance with debugging a registration page I am currently coding. I have hit a roadblock for the past 48 hours and would greatly appreciate any help in resolving the issues. CHALLENGE I am utilizing JavaScript to validate form inputs for error ...

Successive, Interrelated Delayed Invocations

There are two functions in my code, getStudentById(studentId) and getBookTitleById(bookId), which retrieve data through ajax calls. My ultimate goal is to use Deferreds in the following sequence: Retrieve the Student object, Then fetch the Book Title bas ...