This question already has answers here:
Creating multiline strings in JavaScript
(43 answers)
Closed 4 years ago.
I have searched a lot on how to do this, but none of the solutions have been successful in my case.
I have built a very simple JS form which will generate text ready to copy and paste straight into an email.
My only problem now is getting that text appear exactly as I want it in the email (i.e. multi line).
Does anyone have any suggestions based on my code below?
Not duplicate. I have already read/tried the examples in the other post and none of them work. Hence why I am creating a new question specifically for my code.
function myFunction3() {
var b, text;
b = document.getElementById("type").value;
if (b === "rfi") {
text = "This is an RFI email";
} else {
text = "This is a bank details request email";
}
document.getElementById("demo3").innerHTML = text;
}
The HTML part is below:
<p id="demo3"></p>
var a = "First line.";
var b = "Second line.";
console.log(a + "\n" + b);
var a = "First line.";
var b = "Second line.";
document.getElementById("test").innerHTML = a + "<br/>" + b;
<div id="test"></div>
Related
This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed last month.
I've checked many times but since I'm new I'm constantly missing something. In the console, my list is does not appear as like this....and my output is like here
Everything is correct, but is there something I don't know about the console?
let input = prompt("What would you like to do?");
const todos= ['list app finish', 'js practice'];
while (input !== 'quit' && input !=="go away aysel"){
if(input === 'list') {
console.log('***********')
for (let num = 0; num< todos.length; i++){
console.log("${num} ${todos[num]}");
}
console.log('*************')
}
input= prompt('What would you like to do')
}
console.log('Good bye')
Template literals work with the backtick char `.
They do not work with string quotes like ' and "
so you need to change the logging to
console.log(`${num} ${todos[num]}`);
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 4 months ago.
i am trying to create a function that takes a form input (in this case the name), splits it using the space, and then sends either an error message or an all good message. however, it fails to acknowledge conditions for some reason? it always says that its correct even if i only input a name.
function validateName(){
var nameVal = document.getElementById("yourname");
var nameParts = nameVal.toString().split(" ");
if (nameParts.length<2) {
document.getElementById("yourname").nextSibling.innerHTML = "Please enter your First and Last Name.";}
else {
document.getElementById("yourname").nextSibling.innerHTML = "Thank you for entering your First and Last Name!";
}
}
ive tried changing the code in various ways but there is always some error.
Looks like you forgot to add .value in the first line of your function like this document.getElementById("yourname").value.
function validateName(){
var nameVal = document.getElementById("yourname").value;
var nameParts = nameVal.toString().split(" ");
if (nameParts.length<2) {
document.getElementById("yourname").nextSibling.innerHTML = "Please enter your First and Last Name.";}
else {
document.getElementById("yourname").nextSibling.innerHTML = "Thank you for entering your First and Last Name!";
}
}
This question already has answers here:
How do I chop/slice/trim off last character in string using Javascript?
(25 answers)
Closed 7 years ago.
function() {
var address = $("#postcode").val();
var postcode = address.split(' ');
postcode = "Postcode:"+postcode[(postcode.length-2)];
return postcode;
}
This js pulls a postcode value from an online form when the user runs a query. I need to know how I get it to deliver the postcode less the last 2 characters. for example, SP10 2RB needs to return SP102.
Use substring() or substr() or slice().
You have to slice the string and return what you want:
return postcode.slice(0, -2);
// example
postcode = "sample";
// output
"samp"
You can use this function:
function postCode(address)
{
var tmpAddr = address.replace(' ','');
tmpAddr = tmpAddr.substr(0, tmpAddr.length-2);
return tmpAddr;
}
alert(postCode('SP10 2RB'));
This question already has answers here:
Javascript Shorthand for getElementById
(23 answers)
Closed 8 years ago.
My below code is working fine, but I want to know, is there any short method of doing same work only by JavaScript?
<lable for="fstValue"></lable> First number is: <input type="text" id="fstValue" value="" />
<lable for="sndValue"></lable> Second number is: <input type="text" id="sndValue" value="" />
<hr>
<div id="showResult1"></div>
<div id="showResult2"></div>
<div id="showResult3"></div>
<div id="showResult4"></div>
<button onclick="sandeep()">Check Result</button>
// Get the value, calculate and show the value
function sandeep(){
// Define local function - sandeep varriable
var a, b, resPlus, resMinus, resMultiple, resDivide;
// Get First & Second value
a = parseInt(document.getElementById("fstValue").value);
b = parseInt(document.getElementById("sndValue").value);
// Do calculation
resPlus = a + b;
resMinus = a - b;
resMultiple = a * b;
resDivide = a % b;
// Show result
document.getElementById("showResult1").innerHTML="Plus is " + resPlus;
document.getElementById("showResult2").innerHTML="Minus is " + resMinus;
document.getElementById("showResult3").innerHTML="Multiple is " + resMultiple;
document.getElementById("showResult4").innerHTML="Divide is " + resDivide;
}
Can certainly array-drive this. Note my sue of parseInt(*, 10), this is important.
var a = parseInt(document.getElementById("fstValue").value, 10);
var b = parseInt(document.getElementById("sndValue").value, 10);
var texts = ["Plus", "Minus", "Multiple", "Divide"];
var results = [a + b, a - b, a * b, a / b];
for(var i = 0; i < 4; i++ ) {
document.getElementById("showResult" + (i+1) + ").innerHTML=" texts[i] + " is " + results[i];
}
Note you're discovering the problem that frameworks like Angular.js and everything else are attempting to solve. Note the following is pseudo code. It sure would be better if you had a snippet of HTML template
<p>{text} is {result}</p>
And you could put this in a loop
{{iterate over results object}}
<p>{result.text} is {result.results}</p>
{{ end iterate}}
Now far less work has to happen in Javascript. But more importantly, work that should happen in HTML now happens in HTML (data layout), and what happens in JS should happen in JS (getting data). That's the state-of-the-art theory.
You can make your own functions at least:
function getElement(id)
{
return document.getElementById(id);
}
And now you can use just getElement("showResult1");
Also you can do the same for innerHtml and other stuff that you repeat in your code.
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I have no idea why this doesn't work, but doing some validation functions and trying to output a dynamic message to an alert when they hit submit and forgot to fill out a field. It works only on the second click of the submit button, as far as removing the string, everything else works when it should.
Here's the code:
var fname = $('#fname');
var lname = $('#lname');
function validatefname(){
var a = fname.val().length;
if(a < 2) {
fname.prev().addClass("error");
if(msg.search("First Name") == -1) {
msg+= "-Please enter your First Name\n";
}
return false;
} else {
fname.prev().removeClass("error");
msg.replace(/Please enter your First Name\n/g, "");
return true;
}
}
fname.blur(validatefname);
fname.keyup(validatefname);
step4submit.click(function(){
if(validatefname()) {
step4form.submit();
return true
} else {
msg+= "\nPlease fill out the fields marked in red";
alert(msg);
msg = "";
return false;
}
});
String.replace returns a new string, rather than editing the string that makes the replace call.
You need
msg = msg.replace( blah )
In JavaScript, Strings are immutable, they can never change. So if you are calling a function to modify a string in some way, that function will return a new string with your changes, rather than modify the original string.
Change the line that is performing the replacement to save the result of the replacement back into the original variable, like so:
msg = msg.replace(/Please enter your First Name\n/g, "");
(there are various performance and safety reasons for this, but that is for another day/another question).
Try changing
msg.replace(/Please enter your First Name\n/g, "");
to
msg = msg.replace(/Please enter your First Name\n/g, "");