Wednesday, December 9, 2020

[Javascript][example] Trim all input box with type textbox

 html part:

<input type="text" id="st_1" />

<input type="text" id="st_2" />

<input type="text" id="st_3" />


<button onClick="trimAllTextInput()">Save</button>


JavaScript part:

<script>

String.prototype.trim = function() {

    try {

        return this.replace(/^\s+|\s+$/g, "");

    } catch(e) {

        return this;

    }

}


function trimAllTextInput(){

  var inp = document.getElementsByTagName('input');

  for(var i in inp){

    //if(inp[i].type == "text" && inp[i].id.substring(0,3)){

    if(inp[i].type == "text"){

      inp[i].value = inp[i].value.trim();

    }

  }

}

</script>

Reference :

https://stackoverflow.com/questions/196925/what-is-the-best-way-to-trim-in-javascript

1 comment :