I've been working on a new feature for my little project which takes a command from the input (write into input: make a box called somename) and if it detects that the command is "make a box called somename", it will create a div with the class of somename. But, when I try to add styling to the div element, nothing happens. The problem lies in the line above the switch statement's break;
// checks if word is in string s
function wordInString(s, word){
return new RegExp( '\\b' + word + '\\b', 'i').test(s);
}
$('input').keypress(function(e) {
if (e.which == 13) {
let input = $(this).val();
console.log("Initial Input: " + input);
const Commands = ['call it', 'name it', 'make a box called'];
split_input = input.split(" ");
var arrayLength = Commands.length;
for (var i = 0; i < arrayLength; i++) {
command = Commands[i];
console.log(command);
let wordFound = wordInString(input, command);
if (wordFound) {
console.log("Command found: " + command)
switch (command) {
case "make a box called":
let name = split_input[4];
console.log("Box name: " + name)
$('.Dragon').append($('<div>', {class: name}));
// this is the problem line, I do not quite get why it won't w ork
$('.'.concat(name)).addClass({'background-color': 'green', 'height': '50px', 'width': '50px'});
break;
default:
}
}
}
$(this).val('');
}
});
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<!-- Dependencies -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" charset="utf-8"></script>
<script src="https://code.jquery.com/color/jquery.color-2.1.2.min.js" charset="utf-8"></script>
<title>CodeDragon</title>
<link rel="stylesheet" href="/master.css">
</head>
<body>
<div class="Dragon"></div>
<input type="text" name="_input" value="">
<script src="script.js" charset="utf-8"></script>
</body>
</html>