Javascript remove leading character(s) from form data [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to Javascript (and any programming for that matter) and I am looking for a way to take form data and remove leading character(s) from it and re-display them in a list.
For example:
aExample1
bExample2
cExample3
and re-display
Example1
Example2
Example3
Either on the fly or with a button.
(code from comment:
<html>
<textarea name="Text1" cols="20" rows="5">
aExample1 bExample2 cExample3
</textarea>
<input name="Submit" type="submit" value="Submit" />
</html>
)
Thanks in Advance!

You can have a method like this:
function cutFirst(txt) { return txt.substring(1) ;}
It takes a string, and returns all the letters but for the first.
You can play with it, and do something like:
function cutSome(txt,start) { return txt.substring(start) ;}
This will remove the amount of letters that you pass in start (Note: it'll act funny if you don't pass the start though, since it'll be undefined, treated as 0, and you'll get your original string back).
Even better, you can do:
function cutSome(txt,start) {
return start ? txt.substring(start)
: txt.substring(1) ;
}
Cutting out only the first letter if you don't pass the amount start argument.
As for comments, You could do this (i've added an ID , but you can get it by name as well":
<textarea name="Text1" cols="20" rows="5" ID="TextID">
aExample1 bExample2 cExample3
</textarea>
<input name="Submit" type="submit" value="Submit" onclick="trimText();"/>
and have this in you JavaScript:
function trimText() {
var tmpArray= document.getElementById("TextID").value.trim().split(" ");
var returnArray = [];
var len = tmpArray.length;
while (len--) {
returnArray[len] = tmpArray[len].substring(1);
}
document.getElementById("TextID").value = returnArray.join(" ");
}
This will fetch your input, split into words, remove first letter, put back into a string, and change the text area text to that value :)

Related

How do I switch html pages with JavaScript if I don't have the Url [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 hours ago.
This post was edited and submitted for review 17 hours ago.
Improve this question
I have them both as HTML files in my repo. The button is just meant to switch from pages for now. I will add the function for the inputs and conditions later. I just want to get in working before I add anything. There are also no errors that or popping up. I cannot use a tag as I need to change the URL to pass data through it, so I need to have it in JS so I can change it with the data from the input.
The plan is to have the user put in their name in a text input, and have it inside the Url, then have the 2nd page process the name and output a text and photo.
This is the code that I have tried.
const nameButton = document.getElementById("nameBtn");
$(nameButton).click(function(){
preventDefault()
function change_page(){
window.location.replace("page-two.html");
};
change_page()
});
This is the Html:
<input class="font" type="text" id="inputName" value="Type in
your name..."></input>
<button class="fonts" id="nameBtn">Ready?</button>
</form>
</div>
</body>
<script src="scripts/script.js"></script>
</html>
Short answer, only replace the pathname
window.location.pathname = 'page-two.html'
In your case:
const nameButton = document.getElementById("nameBtn");
nameButton.addEventListener('click', function (event) {
event.preventDefault()
window.location.pathname = "page-two.html";
});

Why isn't this working? Javascript, text inputs' values and using them in an if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I think I'm missing something stupidly simple but can't figure out why this isn't working. I'm trying to use 4 values from 4 text inputs and activate an alert when the values = the string value stated.
<div id="AnswersFriends">
<input type="text" align="center" id="TextFriend1" oninput="checkchange">
<input type="text" align="center" id="TextFriend2" oninput="checkchange">
<input type="text" align="center" id="TextFriend3" oninput="checkchange">
<input type="text" align="center" id="TextFriend4" oninput="checkchange">
</div>
<script type="text/javascript">
function checkchange(){
var Ans1 = document.getElementsByID('TextFriend1').value;
var Ans2 = document.getElementsByID('TextFriend2').value;
var Ans3 = document.getElementsByID('TextFriend3').value;
var Ans4 = document.getElementsByID('TextFriend4').value;
if(Ans1=="Joe" && Ans2=="Joe" && Ans3=="Hugo" && Ans4=="Nathan") {
alert("WellDone");
}
}
</script>
change this:
oninput="checkchange"
to this for all the input tags:
oninput="checkchange()"
Also, change this:
document.getElementsByID('TextFriend1').value;
to this:
document.getElementById('TextFriend1').value;
then, do the same for the other getElementById() methods.
To call a function you have to put () after its name
There is no function called getElementsByID, it is getElementById (with a singular element and a lower-case d on the end).

html forum and javascripty [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get an text field text in to an array? Is it possible to get a text from text field and put it in to character array to select each Alphabet and use it in different places?
Like I have a words
Computer
<input type="text" name="name" />
I need it like in different Block Like Name Speel
[C] [o] [m] [p] [u] [t] [e] [r]
Any help appreciated.
What you might want is the following JavaScript:
"Computer".split("")
I think that the main problem is this part: "select each Alphabet and use it in Different Places Like..." so the solution is to use .charAt() function.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "How are you doing today?";
var res = str.charAt(0);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

How to show spaces from textarea [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I'm saving data in a text area on page 1 then showing it on page 2 in a div. However if I write in the textarea
A
B
C
D
It shows it in the div (on page 2) as
A B C D
How do I maintain the line breaks?
Use the CSS property white-space to accept \n as new lines.
div {
white-space: pre;
}
JSFiddle
You can do it with the below code:
function setVal() {
var content = document.getElementById('test').value;
document.getElementById('result').innerHTML = content.replace(/\n/g, '<br/>');
}
Explanation: Basically, the need is to convert the \n characters to their equivalent HTML line breaks using <br/>.
Demo
You need to use this function nl2br() and FYI Don't use nl2br when you save it to the database. Use it when you're displaying the text in HTML.
I would use the <pre> tag which is used for preformatted texts like this on your second page.
An example of which would be this:
<?
echo '<pre>'.$_POST['textarea'].'</pre>';
It preserves both spaces and linebreaks.
$textarea = strip_tags($_POST['textarea']);
echo '<textarea>'.nl2br($textarea).'</textarea>';
nl2br = new line to <br>, so it'll insert <br><br> for every carriage return.
You should use nl2br when displaying the value in div
Online Demo
Here is the code so that you can run it check.
<form>
<textarea name="message" rows="10" cols="50" placeholder="Enter your message here"><?=$_REQUEST['message']?></textarea><br>
<button name="submit" value="submit">Submit</button>
</form>
<h2>Your message will display here</h2>
<div><?=nl2br($_REQUEST['message'])?></div>
<h3>Always remember that don't store nl2br value in database.</h3>

Can't seem to get onclick to work, javascript [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 9 years ago.
Improve this question
I am new to Javascript, I followed a simple tutorial to create a novice calculator, however I can't get it to work and don't understand why not.
It should be displaying the results in an alert box, and it is not.
Any help/input is much appreciated,
Thank you
<script type="text/javascript">
var a,b, result;
funciton setValues()
{
a = Number(document.getElementById("a").value);
b = Number(document.getElementById("b").value);
}
function sum() {
setValues();
result = a+b;
alert("The sume is equel to "+result);
}
function rest()
{
setValues();
result = a-b;
alert("The result is equal to "+result);
}
function mult()
{
setValues();
result = a*b;
alert ("The result is equal to "+result);
}
function div()
{
setValues();
result = a/b;
alert ("The result is equal to "+result);
}
</script>
</head>
<body>
<div>
<input id="a" type="text"/>
<input id="b" type="text"/>
<button type="button" onclick="sum()">+</button>
<button type="button" onclick="rest()">-</button>
<button type="button" onclick="mult()"> *</button>
<button type="button" onclick="div()"> /</button>
</div>
</body>
spelling of function in funciton setValues() is incorrect.
Also Spelling of onclick is incorrect.
Line 39:<button type="button" onlcick="sum()">+</button>
That's the only mistake(s), rest of the code is perfect.
One of them is spelled "onlcick", that one for sure isn't working. That might be why your validation is failing. Did you try it anyways? Let us know if you did or didn't, and whether or not fixing the typo helps.
Edit: You also misspelled function as "funcition" above, as well, which is probably causing some of the trouble as well.
you spelled onclick wrong in your addition button

Categories