Luis Almonte
Luis Almonte

Reputation: 27

Having trouble with input and output in Javascript

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>

Upvotes: 1

Views: 75

Answers (3)

Ilia Choly
Ilia Choly

Reputation: 18557

You need to call the function.

output.value = reverse(input.value);

Upvotes: 2

Luis,

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

In this case:

output.innerHTML = reverse(yourparameter);

Regards.

Upvotes: 1

worldask
worldask

Reputation: 1837

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;

Upvotes: 0

Related Questions