Skip to content

Instantly share code, notes, and snippets.

@josephj
Created March 23, 2014 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephj/9719206 to your computer and use it in GitHub Desktop.
Save josephj/9719206 to your computer and use it in GitHub Desktop.
A Pen by josephj.
<h1>JavaScript Reverse</h1>
<form method="post" id="reverse-form">
<label>
original text:
<input type="text" id="msg" value="abcdefg">
</label>
<br>
<label>
reversed text: <span id="result"></span>
</lael>
</form>
// Append reverse method to String prototype
String.prototype.reverse = function () {
var chars = this.split(''),
result = [],
i;
for (i = chars.length - 1; i >= 0; i--) {
result.push(chars[i]);
}
return result.join('');
};
var $result = $('#result'),
$msg = $('#msg');
function update() {
$result.html($msg.val().reverse());
}
$('#reverse-form').on('input', function (e) {
e.preventDefault();
update();
});
update();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment