How to add line numbers to textarea only with pure Javascript? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
How to do that witn Pure JavaScript?
I need clear code or just guidelines.

From what I gathered from your question this what I believe you need. Hopefully it will get you going in the right direction.
var textAreaID = "user-input";
//turn the text area content into an array
var content = document.getElementById(textAreaID).innerHTML.split("\n");
//create array to hold new Content
var newContent = [];
//loop through and add line numbers
for (var i = 0; i < content.length; i++) { //begin for loop
//append the line numbers and the new value to the newContent array
newContent.push((i + 1) + content[i] + "\n");
} //end for loop
//update the content of textArea with line numbers
document.getElementById(textAreaID).innerHTML = newContent.join("");
<textarea id="user-input" name="user-input" rows="15" cols="40">
Hello is it working?
I think so.
</textarea>

Related

JS function for adding image to all randomly generated text from an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I'm trying to create a function that inserts a randomly chosen word from an existing array into a div. I figured this thing out and now I need to insert an image at the end of every word generated after pressing a button. Any ideas? thanks!
var words = function() {
var wordsArray = new Array('Sun', 'Door', 'Table', 'Moon', 'Sky', 'Chair');
var i;
for (i = 0; i < wordsArray.length; i++) {
var newWords = wordsArray[Math.floor(Math.random() * wordsArray.length)];
document.getElementById('wordhere').innerText = newWords;
}
};
<button class="generate-button" onclick="words();">generate!</button>
<div class="title h4" id="wordhere"></div>
Inside the loop you can replace the statement with:
document.getElementById("wordhere").innerHTML = `${newWords}<img src = "path">`
Basically you can write any node inside that template literal

Shouldn't allow user to enter or copy/paste Emoji characters in any fields [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
my question is probably simple for most but its troubling me and ive been googling for sometime now.
I dont want allow copy or paste content and emoji characters in input fields.
var checkTf = function() {
let charcode_max = 255;
let tf = document.getElementById("tf");
for (let i = tf.value.length - 1; i >= 0; i--) {
if (tf.value[i].charCodeAt(0) > charcode_max) {
tf.value = tf.value.substr(0, i) + tf.value.substr(i + 1);
}
}
}
<html>
<head></head>
<body>
<textarea id="tf" oninput="checkTf()"></textarea>
</body>
</html>
You may increase charcode_max.
https://jsfiddle.net/qx4dt5bz/

Typing effect: Word per Word [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I've been figuring out how to have a typing effect that is word per word instead of letter per letter. So far, all links I've searched are offering letter per letter typewriter effect.
https://macarthur.me/typeit/
https://github.com/mattboldt/typed.js/
Is this possible to achieve? Or have you done something similar?
This is pretty easy.
Just register an interval, split your text, reverse the array and pop the last item. The one you append to the text-container then. Done.
JS
var myText = "Some text you want to be typed automagically..";
var myWords = myText.split(" ").reverse();
var ntrvl = setInterval(function() {
addNextWord();
}, 150);
function addNextWord() {
var nextWord = myWords.pop();
if(nextWord !== undefined) {
var textNow = $(".write-here").text() + " " + nextWord;
$(".write-here").text(textNow);
}
}
What do you think of this?
JSfiddle
create an array with whichever words you want to print one at a time.
const sentence = 'this sentence will be displayed one word at a time';
var arrayOfWords = sentence.split(' ');
for (var i = sentence.length - 1; i >= 0; i--) {
setTimeout(function(){
console.log(sentence[i]); // or display another way
}, 400) // set this to your desired interval
}

Dynamically comparing elements of the array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Can someone please tell me how to compare elements in array with every other element. I mean in array arr = [a,b,c,d];, I would like to compare a with b,c,d , b with a,c,d, etc. And to do that dynamically, no mather of the size of the array.
Try this:
var a=["a","b","c","d"];
for(var i=0;i<a.length;i++){
for(var j=0;j<a.length;j++){
if(i!=j && a[j]===a[i]){
//match, do whatever you want
}
}
}

Script doesn't work with simple arithmetic [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
that's part of my script:
var count = $(".sliderItem").length;
if (count < lp + 5)
{
alert("bp4");
var clips = count-l;
alert("bp5");
}
so the problem is: 'bp4' is visible but 'bp5' not.
when I change var clips = count-1; to var clips = 1; it works fine.
Somebody have some idea?
You don't have var clips = count-1; in your code, you have var clips = count-l;.
Change the letter l (lower case L) to 1 in your code.

Categories