233

How do I transpose a string to lowercase using jQuery? I've tried

var jIsHasKids = $('#chkIsHasKids').attr('checked').toLowerCase();

but it doesn't work. What am I doing wrong?

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
TheBoubou
  • 19,487
  • 54
  • 148
  • 236
  • What does not work? What do you get? What do you want? jQuery does not provide string manipulation functions, so you can't do this with jQuery (but the language itself does). – Felix Kling Apr 11 '11 at 09:16
  • What doesn't work? It doesn't become lower case or there is an error? – Richard Dalton Apr 11 '11 at 09:16

5 Answers5

471

I think you want to lowercase the checked value? Try:

var jIsHasKids = $('#chkIsHasKids:checked').val().toLowerCase();

or you want to check it, then get its value as lowercase:

var jIsHasKids = $('#chkIsHasKids').attr("checked", true).val().toLowerCase();
karim79
  • 339,989
  • 67
  • 413
  • 406
  • for input text work like this: `let toLowercase = function () { jQuery('.tolowercase').on('keypress keydown blur',function () { let currentVal = jQuery(this).val(); jQuery(this).val(currentVal.toLowerCase()); }); };` – rafaelphp Jun 17 '18 at 15:08
26

If it's just for display purposes, you can render the text as upper or lower case in pure CSS, without any Javascript using the text-transform property:

.myclass {
    text-transform: lowercase;
}

See https://developer.mozilla.org/en/CSS/text-transform for more info.

However, note that this doesn't actually change the value to lower case; it just displays it that way. This means that if you examine the contents of the element (ie using Javascript), it will still be in its original format.

Spudley
  • 166,037
  • 39
  • 233
  • 307
13

Try this:

var jIsHasKids = $('#chkIsHasKids').attr('checked');
jIsHasKids = jIsHasKids.toString().toLowerCase();
//OR
jIsHasKids = jIsHasKids.val().toLowerCase();

Possible duplicate with: How do I use jQuery to ignore case when selecting

Community
  • 1
  • 1
r.piesnikowski
  • 2,911
  • 1
  • 26
  • 32
1

$.validator.addMethod("R",function(value,element){

var $firstnames= (value.toLowerCase());
 var $search= ("SHIVA" || "KARTHIK" ==$firstnames  ? false :true ) ;
 return $search ;

}, "This user is blocked");

Shiv kumar K
  • 159
  • 8
0

This question was asked in 2011. I am answering it in September, 2021. Even though I am late by 10 years I am answering for future searchers. Good answers are already given. I will just add an alternate with jQuery's .css property.

$('#chkIsHasKids').attr("checked").val().css("text-transform","lowercase");

This will first check if the id #chkIsHasKids is checked, then will take it's value and then transform it to lower case using the CSS property. That's it!

Relaxing Music
  • 452
  • 4
  • 13