jQuery cannot access a hidden CSS property

I'm working on a form for my new project and have assigned the same class to all input fields. In my CSS file, I've set the text to be capitalized and it displays correctly.

However, when I use jQuery's .val() to get the value of the field, it doesn't show in capitalized form.

var firstName = $('input[name=firstName').val();
var lastName = $('input[name=lastName]').val();

console.log(firstName + " " + lastName);
.MyForm {
  text-transform: capitalize;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form><br>
  <label>First Name</label><input class="MyForm" name="firstName"><br>
  <label>Last Name</label><input class="MyForm" name="lastName"><br>
</form>

When I enter "John" and "Smith" in the input fields, I see them capitalized on the form.

However, in the console, it prints "john smith" in lowercase.

Answer №1

Hey Namir, css is purely for styling purposes. It won't change the actual content of your fields. You might want to consider using javascript for that.

If you want to capitalize the first letter of a string in javascript, you can use string.charAt(0).toUpperCase(). There isn't a built-in Capitalize method in javascript.

Answer №2

When you request the value, it is given as it is - unique and unchanged.

CSS defines the visual appearance of the content as it appears within the specified element.

If you extract the value from the element and view it elsewhere, the CSS styling does not carry over.

To alter the actual value itself (rather than just its appearance), JavaScript is needed for the transformation.

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

Offspring of a parent with a "flex: 1" attribute and a grandparent with a "min-height" property defying the "width: 100%" rule

Is it possible to have a hidden footer just below the screen's bottom and allocate the remaining height to content children without inheriting max-height all the way down? The "Grand child" element is not occupying the full height: body { marg ...

When moving the child grid layout, it often results in unintentionally moving the parent grid layout along with

Currently, I am utilizing React Grid Layout from this repository: https://github.com/STRML/react-grid-layout. My task involves creating a draggable parent div along with a draggable child div. While the functionality works smoothly for the divisions indiv ...

Issues with AJAX requests failing to fire with the latest version of jQuery

A small script I have checks the availability of a username in the database, displaying "taken" if it's already taken and "available" if it's not. The code below works perfectly with jQuery v1.7.2. However, I need to update it for jQuery v3.2.1. ...

Customizing font sizes for individual fonts within the same font family

I am designing a website in a language other than English. This means that the text on the webpage will be a mixture of English and other languages. In order to make sure the text displays correctly, I have set the font-family like this: p{ font-family: ...

Having trouble getting Passport.js middleware to redirect properly with AJAX requests?

I am transferring some data from Javascript to Node through AJAX and JQuery. In Node, I have configured a post request and added a middleware to verify if the user is logged in (redirecting them to the login page if not). However, the res.redirect() in my ...

Utilize Django to leverage a JSON file stored within a context variable for use in jQuery

I need to utilize a list in Jquery and integrate it with the Autocomplete jQueryUI widget. The list is small, so creating a new request seems unnecessary. Therefore, I believe using Jsquery's getJSON is also not required. Here is my current setup: ...

NPM is searching for the package.json file within the user's directory

After completing my test suite, I encountered warnings when adding the test file to the npm scripts in the local package.json. The issue was that the package.json could not be located in the user directory. npm ERR! path C:\Users\chris\pack ...

Issues with JQuery .on() occur when functions are passed as arguments

There seems to be a difference in how the .on() function behaves when passing functions as parameters. Interestingly, defining the function inside the parameters versus passing it separately can have different results. An example of this discrepancy is de ...

Deciphering Encrypted JSON Data

In need of help with our app that generates complex JSON objects where properties and values are in hexadecimal format. How can we convert them back to strings? Example Object: { "636f756e747279": { "6e616d65": "43616e616461", "6 ...

What are some strategies for ensuring that Plotly JS can adapt its height and width to fit the entire div dynamically, without relying on fixed pixel dimensions?

Is there a way to ensure that Plotly automatically adjusts to the size of the container #myDiv without any noticeable delay when the top button is clicked? This code snippet demonstrates a high delay: var z = [], steps = [], i; for (i = 0; i < 500; i+ ...

``Can you share your insights on transferring data from one controller to a service, and then accessing that data on another controller during the same

I'm encountering an issue with passing data from one controller to another using a service. To achieve this, I have implemented prototype inheritance using the $rootScope in my controller and broadcasting the object so that other controllers can acce ...

Issues with the functionality of the bootstrap modal jQuery function in Firefox, causing problems with scrollbars and dynamic modal height adjustments

My newly launched website is built using bootstrap v3 and modals to showcase detailed information about each game server that is queried. Each modal provides insights into the current players on the server, their score, and play time. While implementing ...

Retrieving user input through JavaScript and transmitting information using Ajax

UPDATE: Issue resolved by changing name="demo_name" and similar attributes on my form to id="demo_name". Thanks @Jill I'm attempting to save contact form data into a MySQL database using jquery's ajax feature. I'm new to this, and while it& ...

Using JQuery, remove any duplicate items from one list box and populate another list box

I have two list boxes set up: the leftBox contains all available options, while the rightBox displays the selected options. I am already familiar with how to add and remove items from each list box using jquery. However, my current goal is to automatically ...

Enhancing your JQuery Select2 plugin by incorporating a Checkbox feature

I am currently utilizing the jQuery select2 plugin to enable multiple selections. My goal is to incorporate a checkbox for each selectable option. Depending on whether the checkbox is checked or unchecked, the dropdown option should be selected accordingl ...

Transform a string (variable) into an object using JSON.parse, encountering an unexpected token error

I am struggling with parsing a string variable back to an object. Despite searching through various posts on this issue, I have not found a solution that works for me. if(subMatch.match(/\{.*\}/)){ /// new Object of some sort var o ...

Exploring the Relative Positioning of Two div Elements

Can anyone assist me with finding and comparing the positions of two '<div>' tags using an if statement? I stumbled upon a suggestion in my research, which goes like this: var obstacle = $('#obstacle').css('left', &apo ...

"Exploring the density of the xAxis in Highcharts

Currently, I am incorporating Highcharts into my jsp. Typically, when using Highcharts, we are able to create charts with all points evenly spaced along the x-axis. However, in this instance, I am interested in setting the points based on their x-values ...

Trouble with binding to an array inside a Vue component instance

Trying to grasp the concepts of vue.js, but struggling with a crucial element. The goal is to create an accordion functionality for multiple boxes (only one box displayed at a time; opening a new box closes any previously open ones). Here's the curre ...

I need to update my database by sending requests to my API, as the changes are not being reflected in the current database state. I am eager to see the

Struggling to grasp the concept of making requests to an API that uses express for CRUD operations. In the code snippet below, .get(/bears) displays a form and in app.post('/bears'), I'm using the 'request' module to send a request ...