Click here to Skip to main content
15,348,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So what im trying to do, using two input fields, one from user input and the other for the result, which is a reversed string. I have tried different methods but none of them are seems to be working. When the button is clicked nothing is displayed. Tell me for every mistake that exists in the code!

What I have tried:

<input type="text"id="word">
<input type="button" value="Logarit" id="btn">
<input type="text" id="result">

        function string_Reverse(){

            var x=document.getElementById("word").value;

          var y=  x.split("").reverse().join("");

        document.getElementById("btn").addEventListener("click", string_Reverse);
Updated 15-Dec-20 4:11am
BabyYoda 15-Dec-20 9:51am
I suggest you use your developer tools in the browser and debug it. Should be pretty easy to find the issue.

1 solution

Impossible to answer without seeing the HTML markup.

Given valid markup, your code works fine:
    <input type="text" id="word" />
    <button id="btn">
        Reverse it!
    <output id="result" />
xristos ratsas 15-Dec-20 9:55am
This works fine! But if i replace output with input type="text" then nothing is displayed!

Richard Deeming 15-Dec-20 9:57am
An <input> element doesn't support the innerText property. You'd have to set its value property instead.
document.getElementById("result").value = y;
xristos ratsas 15-Dec-20 10:00am
To be honest i tried that before! But i see now i guess i have misplaced value=y with value:y and maybe that's what has caused the program not to work, now it works just fine. Rating 5 stars for quick answer, thanks a lot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900