0

I'm trying to create a program that can shuffle the middle letters (that is, all letters except the first and last letter in the word) within words. I am trying to pull words out of a string, that is pasted into a text area, in my HTML document, and create an array out of them. From there I'll figure out how to somehow make the shuffling works --but for now my problem is as follows:

    const text = document.getElementById('textfield');
    const textstring = JSON.stringify(text);
  
        document.querySelector('form.input-form').addEventListener('submit', function (e) {
    
        e.preventDefault();

        console.log(text.value);   

        var a1 = new Array();
        a1 = textstring.split(' ');
        console.log(a1);
    
    });
    <!doctype html>
        <html>
            <head> 
                <title>omrokering af bogstaver</title>
                <meta charset="utf-8">
            </head>
            <body id="body">
                <form class="input-form">
                    <input id="textfield" type="text" placeholder="Paste text" />
                    <button type="submit">omrokér</button>
                </form>
                
    
                <script type="text/javascript" src="omrokering.js"></script>
            </body>
        </html>

I can successfully pull the code out of the form, but when I try to log the contents of the array (I have tried a lot of different ways), it prints this: (1) ['{}'] in the console log, after I've typed in the textarea. When I use this alert function:

if(textstring !== null){alert(null);}

It returns "null", so I'm assuming the array is empty, and there is a problem with pulling the string from the form into the array.

I'm very beginner, so it will be greatly appreciated if you can give me a good explanation for this along with a solution, thank you!

2
  • 4
    why are you doing this? const textstring = JSON.stringify(text); Maybe you wanted to do ` const textstring = JSON.stringify(text.value);? Commented May 21, 2021 at 18:15
  • You are stringinfying an DOM element? What? const text = document.getElementById('textfield'); <-- dom element and you do const textstring = JSON.stringify(text); Commented May 21, 2021 at 18:16

2 Answers 2

1

const text = document.getElementById('textfield');

document.querySelector('form.input-form').addEventListener('submit', function (e) {
  e.preventDefault();
  console.log(text.value);   
  var a1 = text.value.split(' ');
  console.log(a1);
});
<form class="input-form">
  <input id="textfield" type="text" placeholder="Paste text">
  <button type="submit">omrokér</button>
</form>
<script src="omrokering.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

You must trim the value, otherwise the resulting array will contain elements with only white spaces. Try this var a1 = text.value.trim().split(' ');
0

Remove textstring that is unnecessary and don't stringify() as it's already a string; and use text element only which you already console.log().

const text = document.getElementById('textfield');
  
document.querySelector('form.input-form').addEventListener('submit', function (e) {
  e.preventDefault();
  console.log(text.value);
  console.log(text.value.split(' '));
});
<!doctype html>
        <html>
            <head> 
                <title>omrokering af bogstaver</title>
                <meta charset="utf-8">
            </head>
            <body id="body">
                <form class="input-form">
                    <input id="textfield" type="text" placeholder="Paste text" />
                    <button type="submit">omrokér</button>
                </form>
                
    
                <script type="text/javascript" src="omrokering.js"></script>
            </body>
        </html>

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.