I am using the following in and attempt to get a user's letter guess to appear on screen, but when I check the devtools the keypress is being returned to the array like [ , , , , , ,]. No letters. Ideas?
var letguessText = document.getElementById("letguess")
var guessArray = []
document.onkeypress = function(event) {
var guess = event.key
guessArray.push(String.fromCharCode(guess));
letguessText.textContent = guessArray;
}
You don't need fromCharCode as the event.key is the character. Here is an example.
var letguessText = document.getElementById("letguess")
var guessArray = []
document.onkeypress = function(event) {
var guess = event.key
guessArray.push(guess);
letguessText.textContent = guessArray;
}
<p id="letguess">Start typing</p>
Related
I have a column in csv file named event_name and I am using Modified Java Script Value step to create new columns if the first character of event_name fufill conditions , for example :
event_name ="app_connection: connection_start_time = 10/05/2018 17:29:26: connection_end_time = 10/05/2018 17:29:29: connection_duration_in_seconds = 3"
==> event = app_connection
==> connection_start_time = 10/05/2018 17:29:26
==> connection_end_time = 10/05/2018 17:29:29
==> connection_duration_in_seconds = 3
if it's : event_name ="scene_access"
==> event = scene_access
I tried this code but it doesn't seem to change anything :
if(event_name.substr(0,3).equals("app"))
{
var event = event_name.substring(0,14);
var connection_start_time = event_name.substr(40,59);
var connection_end_time = event_name.substr(83,102);
var connection_duration_in_seconds = event_name.substr(137,139);
}
else
{
event_name = event_name;
}
If you could give me a hint or explaining what I'm missing will be a huge help.
Thank you.
I tried the field in the substring function, and in the substr function the sintaxis should be substr(string, position_from, number_of_characters).
With this change the code should be:
if(substr(event_name,0,3).equals("app"))
{
var event = substr(event_name,0,14);
var connection_start_time = substr(event_name,40,19);
var connection_end_time = substr(event_name,83,19);
var connection_duration_in_seconds = substr(event_name,137,1);
}
else
{
event_name = event_name;
}
After this press the button "Get variables" to make available the fields in the output of the step.
Trying to make a web page that will get each letter a user inputs and output it in a phonetic alphabet. For example (user types: Hello)(Output: Hotel , Echo , Lima, Lima, Oscar). This is what I have so far just need some guidance on how to get the value of each letter and compare it to like an Array to get the output.
//define UI variables
const userInput = document.querySelector('#input');
const phoneticAlphabet = ["Alpha"," Bravo","Charlie"];
//load all event listeners
loadEventListeners();
function loadEventListeners() {
//add submit event
form.addEventListener('submit', submitInput);
}
//submit user input
function submitInput(e) {
console.log(userInput.value);
if (userInput.value === '') {
alert('Add Input');
}
e.preventDefault();
}
I presume that you would like to replace non-convertible characters from the input. For the same, I am using regular expression. I have also added the response in a "p" tag. And the code runs on clicking "Submit".
Update:
Extended my array for all alphabets :)
Update 2:
Thanks #CharlieBatista for pointing out. Now, the input accepts uppercase characters as well.
//define UI variables
const form = document.phoneticForm;
const userInput = document.querySelector('#input');
const output = document.querySelector('#output');
const phoneticAlphabet = ['Alpha','Bravo','Charlie','Delta','Echo','Foxtrot','Golf','Hotel','India','Juliet','Kilo','Lima','Mike','November','Oscar','Papa','Quebec','Romeo','Sierra','Tango','Uniform','Victor','Whiskey','X-ray','Yankee','Zulu'];
//load all event listeners
loadEventListeners();
function loadEventListeners() {
//add submit event
form.addEventListener('submit', submitInput);
}
//submit user input
function submitInput(e) {
e.preventDefault();
var value = userInput.value;
if (value === '') {
alert('Add Input');
} else {
value = value.replace(/[^a-zA-Z]/gi,'');
userInput.value = value;
value = value.toLowerCase();
var outputArr = [];
for(var i = 0; i < value.length; i++){
outputArr.push(phoneticAlphabet[value.charCodeAt(i)-97]);
}
output.innerHTML = outputArr.join(', ');
}
}
<form name="phoneticForm">
<input type="text" id="input">
<input type="submit">
</form>
<p id="output"></p>
You can use the key property on the keydown event of the field to get the character that was pressed.
Then check if the key is a printable key using key.length === 1 (see this answer).
If the key is printable, convert it to uppercase, then to its character code using String.prototype.charCodeAt() and then subtract 65 from it (character A). This will give you the index in your array.
If this index is within the bounds of the array, access the array and print the character.
const phoneticAlphabet = ['Alpha','Bravo','Charlie','Delta','Echo','Foxtrot','Golf','Hotel','India','Juliet','Kilo','Lima','Mike','November','Oscar','Papa','Quebec','Romeo','Sierra','Tango','Uniform','Victor','Whiskey','X-ray','Yankee','Zulu'];
document.querySelector('#input').addEventListener('keydown', e => {
const isPrintable = e.key.length === 1;
console.clear();
if (isPrintable) {
const idx = e.key.toUpperCase().charCodeAt(0) - 65;
if (idx >= 0 && idx < phoneticAlphabet.length) {
const phoneme = phoneticAlphabet[idx];
console.log(phoneme);
}
}
});
<input type="text" id="input">
I want to detect if a user types one of the strings of my list and then output in what string the user typed in the console.
HTML
<input id="input"></input>
JS
window.onload = function(){
var input = document.getElementById('formula');
var strings = ["string1", "string2", "string3", "string4"]
formula.onkeypress = function (e) {
if (input.value.includes(strings)) {
console.log("Input contains a the string:" + string???)
}
};
}
Try using String.indexOf() and Array.filter()
window.onload = function() {
var strings = ["string1", "string2", "string3", "string4"];
/* replaced `formula` with `input` */
document.getElementById('input').oninput = function(e) {
var find = this.value;
if (0 < find.length) {
var matches = strings.filter(function(s) {
return -1 !== s.indexOf(find);
});
console.log('find string', find, ', matches found', matches);
}
};
};
<input id="input" />
You are fetching element by the wrong ID. The ID of the input is input, and not formula. Ignoring this typo, you need to check the inclusion in the array instead. Replace this:
if (input.value.includes(strings)) {
with this:
if (strings.includes(input.value)) {
Additionally, using keypress event doesn't serve the purpose as the updated value will be fetched after you press one more key. Use oninput instead.
Edit 1:
You can use Array#some to check if the input value contains any string from the array.
window.onload = function() {
var input = document.getElementById('input');
var strings = ["string1", "string2", "string3", "string4"]
input.oninput = function(e) {
if (strings.some(str => input.value.includes(str))) {
console.log("Input contains a the string:" + input.value)
}
};
}
<input id="input"></input>
use like it:-
formula.onkeypress = function (e) {
if (strings.includes(input.value)) {
console.log("Input contains a the string:" + input.value)
}
};
There are several issues in your code:
The id is incorrect for input element
You need to use onkeyup event to get the immediately typed character
You need to use includes() in array type variable.
window.onload = function(){
var input = document.getElementById('formula');
var strings = ["string1", "string2", "string3", "string4"]
input.onkeyup = function (e) {
if (strings.includes(input.value)) {
console.log("Input contains a the string:" + input.value)
}
};
}
<input id="formula" />
If you want to make an autocomplete form : Autocomplete
I want to turn a number I get from a form into a string and combine it with another string, but I receive this:
windows.location='multiSet?type=multi'Number
instead I want to get this:
windows.location='multiSet?type=multiNumber'
Note that the difference is the position of the ' mark is before Number and I want it after it..
All i see in other post is how to put a string as a number not viceversa.
var singleMultiContainer = document.getElementById("singleMultiContainer");
var singleMultiValue = singleMultiContainer.value;
var nextButton = document.getElementById("nextButton");
var multipleSetWindow = "window.location='multiSet.html?type=multi'";
var singleSetWindow = "window.location='SampleInfo.html?type=single'";
var containerCuantity = document.getElementById("containerCuantity");
function setContainers(){
singleMultiValue = singleMultiContainer.value;
containerCuantityValue = parseInt(containerCuantity.value);
if (singleMultiValue == "multi"){
//alert("Multi");
document.getElementById("nextButton").setAttribute("onclick", multipleSetWindow+containerCuantityValue);
} else if (singleMultiValue == "single") {
document.getElementById("nextButton").setAttribute("onclick", singleSetWindow);
}
}
singleMultiContainer.onchange = function(){
setContainers();
}
Don't use setAttribute to define event handlers. And you don't need to parse the value as you want to put it back in a string.
Change
document.getElementById("nextButton").setAttribute("onclick", multipleSetWindow+containerCuantityValue);
to
document.getElementById("nextButton").onclick = function(){
window.location='multiSet.html?type=multi'+containerCuantity.value;
}
I making one application which contains 2 input box and one input button.My qestion is when user enter some input eg. "I used to going somewhere" then my result should be "I UD GNG somewhere." For that i am using this code http://pastebin.com/vHkhASdZ
Please,anyone have idea how to solve then pls reply me asap. I solved my issue in php but in case of javascript i don't have any idea. Here is my output link in php http://codepad.viper-7.com/SQvO6Y
I want my result in javascript.Pls anyone know then give reply.
Use this function
var replaceText = function () {
var inputval = document.getElementById('first_text').value;
var arr = {
"going": "GNG",
"used to": "UD",
"as soon as possible": "ASAP",
"to do fast": "tdf"
}
for(var key in arr) {
if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
}
document.getElementById("second_text").value = inputval;
}
Do something like this. Define word replace as map.
var wordmap = {
going: 'GNG',
'used to': 'UD',
'as soon as possible': 'asap',
'to do fast': 'tdf',
..
}
and define function that iterates every word in the input string.
function replace( text ) {
var input = text.split(' ');
var outputval = []
for (var i in input) {
outputval.push( wordmap[input[i]] || input[i] )
}
return outputval.join(' ');
}
And use it like this
var output = replace( document.getElementById('first_text').value );