<html>
<head>
<script>
//usage
function doReverse(){
var str = document.getElementById( 'mystring' ).value;
alert( "" + reverseString( str ) );
}
//reverse function
function reverseString( str ){
str = str.split(''); //split the entered string
str = str.reverse(); //reverse the order
str = str.join(''); //then join the reverse order array values
return str;
}
</script>
</head>
<body>
<h3>Reverse a String in JavaScript</h3>
<input type="text" id="mystring" value="" />
<button id="btn" onClick="doReverse()" >Reverse</button>
</body>
</html>
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.