We can convert a String to lowercase in jQuery easily by using the JavaScript String toLowerCase() method.
"This is Some Text".toLowerCase();
When working with strings, it can be useful to convert a string to lowercase. A lot of the time when comparing strings, you will need to convert them both to lowercase to compare them since most comparison operators are case sensitive.
In jQuery, we can easily convert a string to lowercase using the toLowerCase() method.
Converting a String to Lowercase In jQuery with a Click
In this simple example, we will let the user input a string, and we will convert it to lowercase.
Here is the HTML setup:
Convert to Lowercase
We will use the jQuery click() method that will run a function we will create. Our function will first use the val() method to get the string the user has entered.
We will then convert the string to lowercase using the String toLowerCase() method.
We will finally post the results using the jQuery text() method.
$('#click-me').click(function(){
//Get the user string
var userString = $("#string1").val();
//Convert the string to lowercase
var lowercaseString = userString.toLowerCase();
//Display the results
$("#results").text(lowercaseString);
});
The final code and output for converting a string to lowercase in jQuery is below:
Code Output:
Full Code:
Convert to Lowercase
<script>
$('#click-me').click(function(){
var userString = $("#string1").val();
var lowercaseString = userString.toLowerCase();
$("#results").text(lowercaseString);
});
</script>
Hopefully this article has been useful for you to learn how to use jQuery to convert a string to lowercase.