Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I repair my HTML code to convert letters to numbers?

I have a code like this:

<!DOCTYPE html>
<html>
  <head>
    <script>
      function replace(){
        var text = document.getElementById("textarea").value;
        var a = text.replace("a", "11");
        var b = text.replace("b", "12");
        var c = text.replace("c", "13");
        document.getElementById("textarea").value = abc;
      }
    </script>
  </head>
  <body>
    <textarea rows="4" cols="50" id="textarea"></textarea>
    <button id="button" onclick="replace();">replace</button>
  </body>
</html>

I’d like to replace letters with numbers, but I don’t know how can I repair this script – only first "var" works or it doesn’t work at all.
I think the problem is here:

document.getElementById("textarea").value = abc;

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is a correct way, you need to

<!DOCTYPE html>
<html>
  <head>
    <script>
      function replace(){
        var text = document.getElementById("textarea").value;
        text = text.replace("a", "11");
        text = text.replace("b", "12");
        text = text.replace("c", "13");
        document.getElementById("textarea").value = text;
      }
    </script>
  </head>
  <body>
    <textarea rows="4" cols="50" id="textarea"></textarea>
    <button id="button" onclick="replace();">replace</button>
  </body>
</html>
  1. replace does not change the text
  2. "concatenation" abc didn’t make too much sense, but you rather wanted to apply in a sequence 3 transformations

here is a playground for you: https://jsfiddle.net/mPrinC/ds9482zn/3/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading