0

I have about 20 pages of forms and I need all the inputs on every page to be converted to uppercase. I'm wondering if it would be possible to build a JavaScript function that I could just copy/paste onto every page without having to do it individually on each input on every page.

Maybe using getElementsByTagName() and addEventLister() and toUpperCase().

Would something like this work?

PS. using CSS only works until the browser send the information back to the server, so that's out.

Pang
  • 9,564
  • 146
  • 81
  • 122
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 2
    You could… but it sounds like it would make more sense to do it when the data reaches the server side. – Quentin Feb 20 '13 at 12:19
  • @Quentin yeah, it would, but it's not as aesthetically pleasing. if i have to i can use php, i'd rather not. – I wrestled a bear once. Feb 20 '13 at 12:32
  • 1
    Apply CSS for the aesthetics. If you want UPPERCASE data, you can't depend on the client to supply it. – Quentin Feb 20 '13 at 12:33
  • @Quentin i was just thinking that but you beat me to the punch. this is what i'll do. i'm still interested in a javascript solution though if anyone has one. – I wrestled a bear once. Feb 20 '13 at 12:34
  • A question like this has already been answered: [change-lowercase-chars-to-uppercase-with-jquery](http://stackoverflow.com/questions/7717099/change-lowercase-chars-to-uppercase-with-jquery) HTH. – Jester Feb 20 '13 at 12:23

1 Answers1

1

Something like this should get you started:

var inputElements = document.querySelectorAll('input, select, textarea');

for (var i = 0, l = inputElements.length; i < l; i++) {
    if (inputElements[i].tagName == 'SELECT') {
        inputElements[i].options[inputElements[i].selectedIndex].value = inputElements[i].options[inputElements[i].selectedIndex].value.toUpperCase();
    } else {
        inputElements[i].value = inputElements[i].value.toUpperCase();
    }
}

Although you should really reconsider this and just do it serverside.

Demo: http://jsfiddle.net/ZgrqA/

PeeHaa
  • 71,436
  • 58
  • 190
  • 262