Reverse a string in html - Javascript String Operation

Javascript examples for String Operation:String Reverse

Description

Reverse a string in html

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>7.23</title> 
      <meta charset="utf-8"> 
      <script>
        String.prototype.reverse=function(){
            return this.split("").reverse().join("");
        }/*www  . j a  v a 2  s.  co m*/
    
      </script> 
   </head> 
   <body> 
      <p> 
         <label for="s">Input</label> 
         <input type="text" id="s" size="25"> 
      </p> 
      <p id="t"></p> 
      <input type="button" 
             id="r" 
             value="Reverse" 
             onclick="document.getElementById('t').innerHTML = document.getElementById('s').value.reverse();">  
   </body>
</html>

Related Tutorials