0

How to change upper case to lower and lower case to upper using if else with jQuery input type with id txt?

$("btn").on("click", function (){ $("txt1").Val(function (i,val) { return val.toUpperCase(); }); }); How to add if condition for checking it uppercase and lower case of Val in input type with id txt1

29th Jan 2022, 8:28 AM
Hrishikesh Shinde
Hrishikesh Shinde - avatar
7 Answers
+ 1
This is what it does if you use function inside, it modify selected element, so if this is what you need, you can choose this way. But if you need to take input value, store it and send to server, than you use method without function inside https://api.jquery.com/val/#val-function https://code.sololearn.com/WvMS6A5aJTVD/?ref=app
29th Jan 2022, 9:28 AM
PanicS
PanicS - avatar
+ 1
Please show your code first
29th Jan 2022, 8:34 AM
Chigozie Anyaeji 🇳🇬
Chigozie Anyaeji 🇳🇬 - avatar
+ 1
To select element by id you need to add # in selector part, if you use val without function it will return just value of input and store on some variable https://code.sololearn.com/WdOAI22qW3ok/?ref=app
29th Jan 2022, 9:16 AM
PanicS
PanicS - avatar
+ 1
$("#btnRev").click(function(){ let r="", t = $("#txt").val(); for (i=0; i<t.length; i++) { if (t[i] == t[i].toUpperCase()) r += t[i].toLowerCase(); else r += t[i].toUpperCase(); } $("#lbl").text(r); }); // I hope this code helps you. https://code.sololearn.com/WzTxUYDagkSA
29th Jan 2022, 10:06 AM
SoloProg
SoloProg - avatar
0
Compare is val is same as val.toUpperCase() if is than it was uppercase and you turn it to lower and vice versa.
29th Jan 2022, 9:04 AM
PanicS
PanicS - avatar
0
$("btn").on("click", function (){ $("txt1").Val(function (i,val) { If($(this).val===$(this).val.toLowerCase()){ return val.toUpperCase(); } else { return val.toLowerCase(); } }); }); Like this but it isn't working.
29th Jan 2022, 9:12 AM
Hrishikesh Shinde
Hrishikesh Shinde - avatar
29th Jan 2022, 9:17 AM
PanicS
PanicS - avatar