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!
const textstring = JSON.stringify(text);Maybe you wanted to do `const textstring = JSON.stringify(text.value);?const text = document.getElementById('textfield');<-- dom element and you doconst textstring = JSON.stringify(text);