Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript convert uppercase to lowercase and vice-versa Reply To: convert uppercase to lowercase and vice-versa

#252909
Mottie
Member

@bearhead The embedded codepen doesn’t appear to be available anymore. Would you please paste the code into the answer?


@Pranab
If you don’t want a loop, use the replace function instead:

https://jsfiddle.net/Mottie/vLmqd35v/1/

var inp = document.querySelector("input");
inp.addEventListener("input", text);

function text(){
  var p = document.querySelector("p");
  var s = inp.value.trim();
  var lc = /[a-z]/;
  p.innerHTML = s.replace(/./g, function(v){
    return lc.test(v) ? v.toUpperCase() : v.toLowerCase();
  });
}