How to make a new line in javascript in a single string - javascript

I am trying to make new lines (in this single line of text) as you can see in my index.html file, but it is not working, any help? (\n is where the new line should start, has not worked either.
index.html:
<html>
<head>
<title>test</title>
</head>
<body bgcolor="black">
<font color="green">
<p id="terminal"></p>
<script>
var text = "this is a test\nthis should be on the next line";
var count = 0;
var speed = 50;
function Type() {
if(count < text.length) {
document.getElementById("terminal").innerHTML += text.charAt(count);
count ++;
setTimeout(Type, speed);
}
}
Type();
</script>

If you don't want to use <br /> you can easily use the <pre> tag. It's actually easier to use <pre> since you don't have to insert the <br /> at the right location in the DOM.
Taken from the docs.
The HTML pre element represents preformatted text which is to be presented exactly as written in the HTML file.
var text = "this is a test\nthis should be on the next line";
var count = 0;
var speed = 50;
function Type() {
if(count < text.length) {
document.getElementById("terminal").innerHTML += text.charAt(count);
count ++;
setTimeout(Type, speed);
}
}
Type();
<html>
<head>
<title>test</title>
</head>
<body bgcolor="black">
<font color="green" />
<pre id="terminal"></pre>
</body>
</html>

Substitute the new line \n with break <br>

Related

How to get all unique characters represantations out of an utf-8 string in Firefox 70?

The result of the following code is only πŸ§œπŸ½β€β™‚οΈ πŸΎπŸΏπŸ›€πŸ›ŒπŸ»πŸΌπŸ§‘πŸ€πŸ‘­πŸ‘©πŸ‘«πŸ‘¨πŸ‘¬, where I want to find all the unique graphic represationations shown in the Firefox output that I copied to the var a originally. (Basically the code should show the original a string as output.)
How to do that?
<html>
<head>
<meta charset="UTF-8" />
<script>
var a = 'πŸ§œπŸ½β€β™‚οΈ πŸ§œπŸΎβ€β™‚οΈ πŸ§œπŸΏβ€β™‚οΈ πŸ§œπŸ›€πŸΏ πŸ›ŒπŸ» πŸ›ŒπŸΌ πŸ›ŒπŸ½ πŸ›ŒπŸΎ πŸ›ŒπŸΏ πŸ§‘πŸ»β€πŸ€β€πŸ§‘πŸ» πŸ§‘πŸ»β€πŸ€β€πŸ§‘πŸΌ πŸ§‘πŸ»β€πŸ€β€πŸ§‘πŸ½ πŸ§‘πŸ»β€πŸ€β€πŸ§‘πŸΎ πŸ§‘πŸ»β€πŸ€β€πŸ§‘πŸΏ πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸ» πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸΌ πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸ½ πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸΎ πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸΏ πŸ§‘πŸ½β€πŸ€β€πŸ§‘πŸ» πŸ§‘πŸ½β€πŸ€β€πŸ§‘πŸΌ πŸ§‘πŸ½β€πŸ€β€πŸ§‘πŸ½ πŸ§‘πŸ½β€πŸ€β€πŸ§‘πŸΎ πŸ§‘πŸ½β€πŸ€β€πŸ§‘πŸΏ πŸ§‘πŸΎβ€πŸ€β€πŸ§‘πŸ» πŸ§‘πŸΎβ€πŸ€β€πŸ§‘πŸΌ πŸ§‘πŸΎβ€πŸ€β€πŸ§‘πŸ½ πŸ§‘πŸΎβ€πŸ€β€πŸ§‘πŸΎ πŸ§‘πŸΎβ€πŸ€β€πŸ§‘πŸΏ πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸ» πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸΌ πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸ½ πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸΎ πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸΏ πŸ‘­πŸ» πŸ‘©πŸ»β€πŸ€β€πŸ‘©πŸΌ πŸ‘©πŸ»β€πŸ€β€πŸ‘©πŸ½ πŸ‘©πŸ»β€πŸ€β€πŸ‘©πŸΎ πŸ‘©πŸ»β€πŸ€β€πŸ‘©πŸΏ πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸ» πŸ‘­πŸΌ πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸ½ πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸΎ πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸΏ πŸ‘©πŸ½β€πŸ€β€πŸ‘©πŸ» πŸ‘©πŸ½β€πŸ€β€πŸ‘©πŸΌ πŸ‘­πŸ½ πŸ‘©πŸ½β€πŸ€β€πŸ‘©πŸΎ πŸ‘©πŸ½β€πŸ€β€πŸ‘©πŸΏ πŸ‘©πŸΎβ€πŸ€β€πŸ‘©πŸ» πŸ‘©πŸΎβ€πŸ€β€πŸ‘©πŸΌ πŸ‘©πŸΎβ€πŸ€β€πŸ‘©πŸ½ πŸ‘­πŸΎ πŸ‘©πŸΎβ€πŸ€β€πŸ‘©πŸΏ πŸ‘©πŸΏβ€πŸ€β€πŸ‘©πŸ» πŸ‘©πŸΏβ€πŸ€β€πŸ‘©πŸΌ πŸ‘©πŸΏβ€πŸ€β€πŸ‘©πŸ½ πŸ‘©πŸΏβ€πŸ€β€πŸ‘©πŸΎ πŸ‘­πŸΏ πŸ‘«πŸ» πŸ‘©πŸ»β€πŸ€β€πŸ‘¨πŸΌ πŸ‘©πŸ»β€πŸ€β€πŸ‘¨πŸ½ πŸ‘©πŸ»β€πŸ€β€πŸ‘¨πŸΎ πŸ‘©πŸ»β€πŸ€β€πŸ‘¨πŸΏ πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸ» πŸ‘«πŸΌ πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸ½ πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸΎ πŸ‘©πŸΌβ€πŸ€β€πŸ‘¨πŸΏ πŸ‘©πŸ½β€πŸ€β€πŸ‘¨πŸ» πŸ‘©πŸ½β€πŸ€β€πŸ‘¨πŸΌ πŸ‘«πŸ½ πŸ‘©πŸ½β€πŸ€β€πŸ‘¨πŸΎ πŸ‘©πŸ½β€πŸ€β€πŸ‘¨πŸΏ πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸ» πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸΌ πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸ½ πŸ‘«πŸΎ πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸΏ πŸ‘©πŸΏβ€πŸ€β€πŸ‘¨πŸ» πŸ‘©πŸΏβ€πŸ€β€πŸ‘¨πŸΌ πŸ‘©πŸΏβ€πŸ€β€πŸ‘¨πŸ½ πŸ‘©πŸΏβ€πŸ€β€πŸ‘¨πŸΎ πŸ‘«πŸΏ πŸ‘¬πŸ» πŸ‘¨πŸ»β€πŸ€β€πŸ‘¨πŸΌ πŸ‘¨πŸ»β€πŸ€β€πŸ‘¨πŸ½ πŸ‘¨πŸ»β€πŸ€β€πŸ‘¨πŸΎ πŸ‘¨πŸ»β€πŸ€β€πŸ‘¨πŸΏ πŸ‘¨πŸΌβ€πŸ€β€πŸ‘¨πŸ» πŸ‘¬πŸΌ πŸ‘¨πŸΌβ€πŸ€β€πŸ‘¨πŸ½ πŸ‘¨πŸΌβ€πŸ€β€πŸ‘¨πŸΎ πŸ‘¨πŸΌβ€πŸ€β€πŸ‘¨πŸΏ πŸ‘¨πŸ½β€πŸ€β€πŸ‘¨πŸ» πŸ‘¨πŸ½β€πŸ€β€πŸ‘¨πŸΌ πŸ‘¬πŸ½ πŸ‘¨πŸ½β€πŸ€β€πŸ‘¨πŸΎ πŸ‘¨πŸ½β€πŸ€β€πŸ‘¨πŸΏ πŸ‘¨πŸΎβ€πŸ€β€πŸ‘¨πŸ» πŸ‘¨πŸΎβ€πŸ€β€πŸ‘¨πŸΌ πŸ‘¨πŸΎβ€πŸ€β€πŸ‘¨πŸ½ πŸ‘¬πŸΎ πŸ‘¨πŸΎβ€πŸ€β€πŸ‘¨πŸΏ πŸ‘¨πŸΏβ€πŸ€β€πŸ‘¨πŸ» πŸ‘¨πŸΏβ€πŸ€β€πŸ‘¨πŸΌ πŸ‘¨πŸΏβ€πŸ€β€πŸ‘¨πŸ½ πŸ‘¨πŸΏβ€πŸ€β€πŸ‘¨πŸΎ πŸ‘¬πŸΏ 🏻 🏼 🏽 🏾 🏿 ';
var b = [...new Set([...a])];
var c = b.join('');
function init() { document.getElementById('result').innerHTML = c; }
</script>
</head><body onload="init()">
<span id="result"></span>
</body>
</html>

Word counting in Javascript

I am trying to figure out a way to count words that are placed in multiple paragraph blocks in javascript. Right now I have a button that is connected to a function and that function is linked to an ID in the paragraph. Here is my code
function processText(elements) {
var count = 0;
for (var i = 0; i < elements.length; i++) {
count += elements[i].textContent.split(/\s/).length;
}
return count;
}
var wordsInParagraphs = processText(document.getElementsByTagName("data"));
<!DOCTYPE html>
<html>
<head>
<meta name="title" content="The Cask of Amontillado--Edgar Allan Poe (1809-1849)">
</head>
<body>
<p><button 1="processText(elements);">Process</button></p>
<p id="data"></p>
</body>
Is this what you're looking for? You just need to call the function on click and grab all the elements you want to count, you have the rest there (I'm using split instead of regex).
function processText() {
var elements = document.querySelectorAll(".data");
var count = 0;
for (var i = 0; i < elements.length; i++) {
count += elements[i].textContent.split(" ").length;
}
console.log(count)
}
<!DOCTYPE html>
<html>
<head>
<meta name="title" content="The Cask of Amontillado--Edgar Allan Poe (1809-1849)">
</head>
<body>
<p><button onclick="processText();">Process</button></p>
<p class="data">text in paragraph one</p>
<p class="data">text in paragraph two</p>
</body>
The markup has some problems, for example, 1="processText(elements);" probably you meant onClick="processText(elements);", however, you're passing a param called elements. Further, you have a tag with id="data" and you're trying to look for tag name those elements.
A better approach is using the function addEventListener for a better logic and you should mark those paragraphs using a class name class="data". Finally, for splitting by spaces use this regex /\s+/
function processText(elements) {
var count = 0;
for (var i = 0; i < elements.length; i++) {
count += elements[i].textContent.split(/\s+/).length;
}
return count;
}
document.getElementById('myButton').addEventListener('click', function() {
var wordsInParagraphs = processText(document.getElementsByClassName("data"));
document.getElementById('total').textContent = wordsInParagraphs;
});
<p><button id='myButton'>Process</button></p>
<p class="data">Ele from Stack</p>
<p class="data">Ele from Venezuela</p>
<p id='total'></p>

How do I select text between two characters in Javascript?

So I am currently trying to find out how to select text between two characters(for the example I will use a slash / )
Here is what I have so far.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function startWhenLoaded() {
var text = $("p").text();
var secondStartingPoint, startPointOne, startPointTwo;
if (text.indexOf("/", 0) !== -1) {
//I did -1 because indexOf returns -1 if nothing is found.
/*Also, the second argument in indexOf() acts as my starting
point for searching.*/
secondStartingPoint = text.indexOf("/") + 1;
startPointOne = text.indexOf("/") + 1;
if (text.indexOf("/", secondStartingPoint) !== -1) {
startPointTwo = text.indexOf("", secondStartingPoint) + 1;
var selectedText = slice(startPointOne, startPointTwo);
$("body").append("<p>" + selectedText + "</p>");
//but nothing happens.
}
}
</script>
</head>
<body onload="startWhenLoaded()">
<p>/can I select and duplicate this?/</p>
</body>
</html>
But it doesn't do anything.
It could be achieved simply by using a regex like :
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function startWhenLoaded() {
var text = $("p").text();
var extracted = text.match(/\/(.*)\//).pop();
alert('The extracted text is :'+extracted);
}
</script>
</head>
<body onload="startWhenLoaded()">
<p>Some text here in the start /can I select and duplicate this?/ Some extra text at the end</p>
</body>
</html>
Regex is simplest and easiest way to get your solution.
use exec() function to get text between '/';
console.log(/^\/(.*)\/$/.exec('/some text, /other example//'))

Javascript change text of all inputs

I'm really newbie at Web Development and I'm trying to change the text of some inputs, with Javascript. Here is a example of what my code have to do
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "R$" with "" in the field below:</p>
<input id="demo" value="R$ 1223,43"></input>
<input id="demo1" value="R$ 134523,67"></input>
<input id="demo2" value="R$ 12453,41"></input>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var x=document.getElementByTagName("input")
for(var i = 0; i < x.length; i++) {
var str=x[i].innerHTML;
var n=str.replace(",",".");
var n1 = n.replace("R$ ","");
document.getElementById("demo").innerHTML=n1;
}
}
</script>
</body>
</html>
So, I want to withdraw the "R$" and replace "," to "." for some math operations. And I have to do this with all inputs in my code.
You were nearly there, replacing a few things to make it look similar to this:
function myFunction() {
var x = document.getElementsByTagName("input"); // ; was missing and you used getElementByTagName instead of getElementsByTagName
for (var i = 0; i < x.length; i++) {
var str = x[i].value; // use .value
var n = str.replace(",", ".");
var n1 = n.replace("R$ ", "");
//document.getElementById("demo").innerHTML=n1; // use x[i] again instead
x[i].value = n1; // and again use .value
}
}
DEMO - Running updated code
These are the needed steps - at least step 1 through 3
moved the script to the head where it belongs
changed getElementByTagName to getElementsByTagName, plural
get and change x[i].value
chained the replace
DEMO
<!DOCTYPE html>
<html>
<head>
<title>Replace example</title>
<script>
function myFunction() {
var x=document.getElementsByTagName("input"); // plural
for(var i = 0; i < x.length; i++) {
var str=x[i].value;
x[i].value=str.replace(",",".").replace("R$ ","");
}
}
</script>
</head>
<body>
<p>Click the button to replace "R$" with "" in the field below:</p>
<input id="demo" value="R$ 1223,43"></input>
<input id="demo1" value="R$ 134523,67"></input>
<input id="demo2" value="R$ 12453,41"></input>
<button onclick="myFunction()">Try it</button>
</body>
</html>
First of all, use .value instead of .innerHTML. .innerHTML referes to text within the opening and closing of the tag.
Secondly, correct the spellings at var x=document.getElementByTagName("input")
it should be getElementsByTagName
this function should do what you want:
function myFunction()
{
var eles=document.getElementsByTagName("input");
for(var i = 0; i < eles.length; i++)
{
if(eles[i].type != 'text') continue; // inputs that aren't of type text dont make sense here
var str = eles[i].value;
str=str.replace(",",".");
str=str.replace("R$ ","");
eles[i].value=str;
}
}

how to generate words onclick?

I am trying to create an html page that contains a title, and when we click on the title it generates words underneath it. But the code I have is only working for the first click, and it's also deleting the title. So my question is, how can I make it generate words under the title without deleting it?
<!DOCTYPE html>
<html>
<head>
<title>function JavaScript</title>
<script>
var k = 0;
function bla(){
var ph = ["red ","blue","black","green","yellow"];
if(k <= ph.length ){
document.write(ph[k]);
k++;
}
}
</script>
</head>
<body>
<h1 onclick="bla();">Click here</h1>
</body>
</html>
Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. This is almost certainly not what you intend to have happen. You should therefore avoid using document.write in situations such as this
so try add a element and then write into them like this
<h1 onclick="bla();">Click here</h1>
<span id="test"></span>
JS:
if(k <= ph.length ){
//document.write(ph[k]);
document.getElementById("test").innerText+=" "+ ph[k];;
k++;
}
JS Fiddle Example
This may be what you need:
var k = 0;
function bla() {
var ph = ["red", "blue", "black", "green", "yellow"];
if (k < ph.length) {
var p = document.createElement('p');
p.innerHTML = ph[k];
document.body.appendChild(p);
k++;
}
}
http://jsfiddle.net/p5rLX/
It will write each word into its own paragraph.

Categories

Resources