1

I have recently started doing my own project with javascript and I ran into a roadblock. I'm doing a reverse string project where the user inputs a string and the output reverses it. Now my problem is that I can't get the reverse string to show up in the output area.

The Javascript part:

  <script>
    function pass() {
      var input = document.getElementById('inputfield');
      var output = document.getElementById('results');

      var string = input.value;

      var reverse = function (string) {
        return string.split('').reverse().join('');
      };
      output.innerHTML = reverse;
    }
  </script>

The HTML:

<div id="reverse">
 <h1>Type Below:</h1>

  <form name="form">
   <input type="text" name="inputfield" id="inputfield">
   <button onClick="pass();">Submit</button></br>
   <input type="text" name="out" placeholder="results" id="results">

 </form>
</div>
3
  • Use output.firstChild.nodeValue, innerHTML is a proprietary Microsoft JScript method and is not reliable when reading/writing values at an application level so don't use it and get sucked in to the false sense that it is reliable. Commented Sep 12, 2014 at 14:26
  • @John: It's not proprietary anymore, and I don't think there are any problems in browsers. But I'm happy to learn something new, so what's a potential problem? Commented Sep 12, 2014 at 14:54
  • @FelixKling It treats code as text, not code. Failure to properly serialize means for example referencing an element with an ID dumped on to the DOM instead of using proper methods (importNode, insertBefore, etc) the DOM will not see that element. Just because everyone finds jumping off a bridge easy doesn't mean you should jump off a bridge or use what is and still remains a unreliable proprietary method. Commented Sep 13, 2014 at 3:47

3 Answers 3

2

You need to call the function.

output.value = reverse(input.value);
Sign up to request clarification or add additional context in comments.

1 Comment

I did this but still nothing shows up in the output field.
1

Luis,

When creating one function that receive an parameter, never forget to send this parameter.

In this case:

output.innerHTML = reverse(yourparameter);

Regards.

Comments

0

you will get reversed string like this:

output.innerHTML = reverse(string);

because

  var reverse = function (string) {
    return string.split('').reverse().join('');
  };

is just a function declaration, the string is only a parameter of the function, but not equal to

  var string = input.value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.