Created
March 23, 2014 05:35
-
-
Save josephj/9719206 to your computer and use it in GitHub Desktop.
A Pen by josephj.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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