Remove all the occurrences of and , using javascript from an input field - javascript

I have an input field of type text with the following description
<input type="text" id="inputID"/>
It has the following content
hello and , world and , world hello and world and ,
With the content on the above input field, I am trying to remove all the occurrences of and , using javascript.
Presently, I am doing this but don't know how to pass it to the input field using the element id="inputID" of the input tag
var message= content.replace(/[&]nbsp[;][,]/gi," ");

Your regex must be / |,/g ie any or , in the string. g stand for global search.
var inputValue = document.getElementById("inputID").value ;
var cleanedValue= inputValue.replace(/ |,/g," ");
document.getElementById("inputID").value = cleanedValue;
<input type="text" id="inputID" value="hello and , world and , world hello and world and ,"/>
Or if you find one liner code more fancy then do this.
document.getElementById("inputID").value = document.getElementById("inputID").value.replace(/ |,/g," ");;

Related

Adding a word in the middle of a sentence with User Input Javascript

Can someone help me to make html and javascript about adding a word in the middle of a sentence with user input? Thanks...
Sentence:
Aku sedang [User Input] disekolah.
Input Box | Button to make sentence
the result of the sentence that has been added to the word and entered by the user
ex:
Aku sedang makan disekolah.
for the html body:
<p>Aku sedang <span id="sentence">_____</span> di sekolah.</p>
<input type="text" id="userInput"> <button onclick="readMore()">modify</button>
then add the script
<script>
function readMore(){
let word = document.getElementById("userInput").value;
document.getElementById("sentence").innerHTML = word;
}
</script>
you can read other usability of HTML DOM in here

Get input[text] value before special characters are trimmed in typescript

I have a from. User can fill an input field with text that contains special characters like \n, \t and etc. I have to replace these special characters and add it back as value of the input field.
for example:
user input is: hello, \n how are you doing? \t this is a sample text.
This is what I need: hello, newline how are you doing? tab this is a sample text.
I am using JQuery and Typescript 2.4
How about this?
$('#input').keyup(function () {
var val = $(this).val();
var replaceWithBr = val.replace(/\\n/g, '<br>');
var replaceWithTab = replaceWithBr.replace(/\\t/g, ' ');
$('#result').html(replaceWithTab);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input">
<div id="result"></div>

JavaScript - split groups of strings between ( ) and put them inside two separated inputs

I have this line:
first (word1-word2-word3) second (any1-any2-any3)
Lets say I have a textarea and two inputs (input1 and input2).
When I paste the line above inside the textarea, and by using JavaScript, I want:
word1-word2-word3
to go to the first input (input1) and for the other part;
any1-any2-any3
to go to the second input (input2).
Thank you.
You could use a regular expression and take the two matching groups.
function split(v) {
var m = v.match(/^.*\((.*)\).*\((.*)\)$/);
document.getElementById('input1').value = m[1];
document.getElementById('input2').value = m[2];
}
<textarea onchange="split(this.value)"></textarea><br>
<input type="text" id="input1"><br>
<input type="text" id="input2">

Calling up html text box using javascript and converting it to an integer

So I just want to understand how I could do this, very simple example:
My html is
<div id = "pie"> eee </div>
<input type="text" name="item1" size="5">
and my javascript is
var q = parseFloat("10");
document.getElementById("pie").innerHTML = q ;
and basically what I want to do is call up whatever text is in the text box (named item1) and convert it to an integer, then display that number. I'm sure there's an easy way to do it but I cant figure it out
Try this:
+(document.getElementsByName('item1')[0].value);
That is the shortest way to do that now to display it, for an example in the element #pie
document.getElementById('pie').innerHTML = +(document.getElementsByName('item1')[0].value)
Putting + before something will convert it to a number. If the text box isn't a number you can detect that by:
var value = document.getElementsByName('item1')[0].value;
document.getElementById('pie').innerHTML = !isNaN(value)?+value:"Not a number";
Demo
Get the value of item 1
var item1_value = parseInt(getElementsByName('item1')[0].value);
Display the value of item1 in pie
document.getElementById('pie').innerHTML = item1_value;

how to convert form text input to ASCII in Javascript?

how to convert text input to ASCII and display in text area..
HTML
<div class="form-group">
<label for="exampleInputPassword1">Favorite Food</label>
<input type="text" class="form-control" id="fFood" placeholder="Favorite Food" required>
<textarea name="txt_output"></textarea>
</div>
I assume what you want mean is to display the "text" typed in textBox in textArea
If so, and here you go: try here by clicking the button to display text in text area
The JS:
function display(){
var result = document.getElementById('fFood');
var txtArea = document.getElementById('textArea');
txtArea.value = result.value;
}
EDIT if you want to get the ASCII code from a string: try it here.
source of reference
If what you mean is how do you get the character code of a character from a javascript string, then you would use the string method str.charCodeAt(index).
var str = "abcd";
var code = str.charCodeAt(0);
This will technically be the unicode value of the character, but for regular ascii characters, that is the same value as the ascii value.
Working demo: http://jsfiddle.net/jfriend00/ZLRZ7/
If what you mean is how you get the text out of a textarea field, you can do that by first getting the DOM object that represents that object and then by getting the text from that object:
var textareas = document.getElementsByName("txt_output");
var txt = textareas[0].value;
If you then want to put that text into the input field, you can do that with this additional line of code:
document.getElementById("fFood").value = txt;
jquery
$(function(){
$('input[type=text]').keyup(function(){
var x = $('input[type=text]').val();
$('textarea').val(x);
});
});
javascript
<script>
function myFunction()
{
var x = document.getElementById("fFood").value;
document.getElementById("ta").value=x;
}
</script>
and html
<div class="form-group">
<label for="exampleInputPassword1">Favorite Food</label>
<input type="text" class="form-control" id="fFood" onkeyup="myFunction()" placeholder="Favorite Food">
<textarea name="txt_output" id="ta"></textarea>
</div>

Categories