Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  18] [ 3]  / answers: 1 / hits: 98902  / 15 Years ago, fri, october 23, 2009, 12:00:00

I'm trying to reverse an input string



var oneway = document.getElementById('input_field').value();
var backway = oneway.reverse();


but firebug is telling me that oneway.reverse() is not a function. Any ideas?



Thank you


More From » string

 Answers
23

reverse() is a method of array instances. It won't directly work on a string. You should first split the characters of the string into an array, reverse the array and then join back into a string:



var backway = oneway.split().reverse().join();


Update



The method above is only safe for regular strings. Please see comment by Mathias Bynens below and also his answer for a safe reverse method.


[#98460] Monday, October 19, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deannaalysonl

Total Points: 703
Total Questions: 101
Total Answers: 115

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
deannaalysonl questions
;