I'm sending variables to a text box as a concatenated string so I can include multiple variables in on getElementById call.
I need to specify a line break so the address is formatted properly.
document.getElementById("address_box").value =
(title + address + address2 + address3 + address4);
I've already tried \n after the line break and after the variable. and tried changing the concatenation operator to +=.
Fixed: This problem was resolved using;
document.getElementById("address_box").value =
(title + "\n" + address + "\n" + address2 + "\n" + address3 + "\n" + address4);
and changing the textbox from 'input type' to 'textarea'
You can't have multiple lines in a text box, you need a textarea. Then it works with \n between the values.
document.getElementById("address_box").value =
(title + "\n" + address + "\n" + address2 + "\n" + address3 + "\n" + address4);
You need to use \n inside quotes.
document.getElementById("address_box").value = (title + "\n" + address + "\n" + address2 + "\n" + address3 + "\n" + address4)
\n is called a EOL or line-break, \n is a common EOL marker and is commonly refereed to as LF or line-feed, it is a special ASCII character
Using Backtick
Backticks are commonly used for multi-line strings or when you want to interpolate an expression within your string
let title = 'John';
let address = 'address';
let address2 = 'address2222';
let address3 = 'address33333';
let address4 = 'address44444';
document.getElementById("address_box").innerText = `${title}
${address}
${address2}
${address3}
${address4}`;
<div id="address_box">
</div>
Related
I want to make alert message show the data of the form I input.But the text element's data doesn't work correctly.
<script type="text/javascript">
function showUserData(){
category = document.getElementById("category").value;
regicon = "";
var obj=document.getElementsByName("register");
for(idx in obj){
if(obj[idx].checked){
regicon += obj[idx].value;
}
}
title = document.getElementById("title").value;//here is the problem
author = document.getElementById("author").value;
email = document.getElementById("email").value;
content = document.getElementById("content").value;
password = document.getElementById("password").value;
coverdate = document.getElementById("coverdate").value;
contentimage = document.getElementById("contentimage").value;//
time_result = new Date();//기사등록일은 date()
window.alert("카테고리: "+category + "\n"
+ "등록상태: " + regicon + "\n"
+ "제목: " + title + "\n"
+ "이메일: " + email + "\n"
+ "기자: " + author + "\n"
+ "내용: " + content + "\n"
+ "취재일: " + coverdate + "\n"
+ "기사등록일: " + time_result + "\n");
}
</script>
When I run the code, the alert show 'undefined' at the title value.
I thought that the title value doesn't initialized so I tried
title = "";
It was not the solution.How can I make the function run correctly
Have you created a titlevariable? It seems to me you forgot to put "var" or "const" before the title variable.
I am trying to set the content of a div which has the id "results" to have the value of a variable (i). I also want to set the ID of the element to the value of another variable (a).
var i="somecontent";
var a=0;
document.getElementById("results").innerHTML += "<p "
+ "id=" + """ + a + "" " + "\>" + i + "<p/>
This however, is resulting in the ID of the <P> tag being id=""0"" rather than id="0".
Here is the output:
<p id=""0"">somecontent</p>
However, I want to display:
<p id="0">somecontent</p>
What can I do to remove the extra quotations?
Those extra quotation marks are the " character references you put in. Character references are HTML and therefore parsed by the HTML parser, not the JavaScript parser. Since " is a way of representing a literal double quote character without having it be treated specially by the HTML parser, the parser thinks you intended for it to be part of the attribute value, and that's what you get.
If you were intending to add escaped quotes to your HTML that don't interfere with the JavaScript parser, escape them with backslashes or use single quotes instead:
document.getElementById("results").innerHTML += "<p id=\"" + a + "\">" + i + "</p>";
document.getElementById("results").innerHTML += "<p id='" + a + "'>" + i + "</p>";
You can also leave them out entirely, unless you're writing XHTML for some reason:
document.getElementById("results").innerHTML += "<p id=" + a + ">" + i + "</p>";
Try this:
document.getElementById("myDiv").innerHTML += "<p "
+ "id=" + a + "\>" + i + "<p/>";
A better way: "id=\"" + a + "\"" + "\>" + i + "
You only need to use the html codes like " in your html.
Javascript handles " just fine.
Removed html codes:
var i="somecontent";
var a=0;
document.getElementById("results").innerHTML += "<p " + "id=\'" + a + "\'/>" + i + "</p>"
Use createElement, createTextNode and appendChild
I do not recommend numeric IDs - they will come back and bite you.
This is a preferred method - I assume you have a loop
window.onload=function() {
var par,text,res = document.getElementById("results");
var content = ["Paragraph1","Paragraph2","Paragraph3"];
for (var i=0;i<content.length;i++) {
par = document.createElement("p");
par.id = "A"+i;
text = document.createTextNode(content[i]);
par.appendChild(text);
res.appendChild(par);
}
}
<div id="results"></div>
Your inner HTML quotes are misplaced. Try this:
var i = "somecontent";
var a="0";
var j = document.getElementById("results").innerHTML += '<p id="' + a + '">' + i + '</p>';
console.log(i);
console.log(j);
<Div id="results"></Div>
I use a JavaScript blob to create an FDF file which opens & fills in a locally stored PDF.
However, the file path to the locally stored PDF contains an accented character (and I am unable to edit the folder name).
This code works when the folder path doesn’t contain an accent and if I open the fdf in Notepad, the default encoding is ANSI. But when the folder path contains an accent, the FDF opens to a message stating the PDF cannot be found. Furthermore, the default encoding in Notepad has changed to UTF-8.
FDF_Text = ''
+ '%FDF-1.2' + "\n"
+ '1 0 obj<</FDF<</F(T:/Échange/MY_PDF.pdf)/Fields 2 0 R>>>>' + "\n"
+ 'endobj' + "\n"
+ '2 0 obj[' + "\n"
+ '<</T(FIELD_NAME)/V(SOME_TEXT)>>' + "\n"
+ ']' + "\n"
+ 'endobj' + "\n"
+ 'trailer' + "\n"
+ '<</Root 1 0 R>>' + "\n"
+ '%%EO'
var blobObject = new Blob([FDF_Text], {type: 'text/css;charset=ANSI'});
window.navigator.msSaveOrOpenBlob(blobObject, 'MY_FDF.fdf');
I have tried
replacing É with E
using String.fromCharCode(201) (the chr value for É)
changing & removing the "type" of the blob itself to several different examples I've found (sorry I didn't keep track of all the different combinations).
Can anyone suggest a different solution?
You can represent the data as binary, just run through the string and fill a binary array
FDF_Text = ''
+ '%FDF-1.2' + "\n"
+ '1 0 obj<</FDF<</F(T:/Échange/MY_PDF.pdf)/Fields 2 0 R>>>>' + "\n"
+ 'endobj' + "\n"
+ '2 0 obj[' + "\n"
+ '<</T(FIELD_NAME)/V(SOME_TEXT)>>' + "\n"
+ ']' + "\n"
+ 'endobj' + "\n"
+ 'trailer' + "\n"
+ '<</Root 1 0 R>>' + "\n"
+ '%%EO'
var uint8 = new Uint8Array(FDF_Text.length);
for (var i = 0; i < uint8.length; i++){
uint8[i] = FDF_Text.charCodeAt(i);
}
var blobObject = new Blob([uint8], {type: 'text/fdf'});
window.navigator.msSaveOrOpenBlob(blobObject, 'MY_FDF.fdf');
I have a Google Apps Script I am writing that combines a number of reports into a consolidated tab-delimited (TSV) format. However, I am noticing that whenever I write to file, the tabs in my strings are being converted into spaces. Is there some other string literal that I should use?
var content =
data.parameters.time[0] + "\t" +
data.parameters.url[0] + "\t" +
data.parameters.code_name[0] + "\t" +
data.parameters.app_name[0] + "\t" +
data.parameters.version[0] + "\t" +
data.parameters.cookies[0] + "\t" +
data.parameters.lang[0] + "\t" +
data.parameters.platform[0] + "\t" +
data.parameters.user_agent[0] + "\t" +
data.parameters.ex[0];
fld.createFile(makeid() + ".tab", content, ContentService.MimeType.CSV);
Nothing is wrong with using the \t string literal - rather you're using the wrong MimeType enum, from the ContentService. DriveApp methods expect MimeType as a string. Try:
fld.createFile(makeid() + ".tab", content, MimeType.CSV);
Here's a snippet that shows the tab characters survive the file write when the MimeType is properly set:
function myFunction() {
var content =
'time' + "\t" +
'url' + "\t" +
'code_name' + "\t" +
'etcetera';
Logger.log("Initial TSV: " + JSON.stringify(content.split("\t")));
var newFile = DriveApp.createFile("Delete-me" + ".tab", content, MimeType.CSV);
// Check our result, by reading back the file
var blob = newFile.getBlob();
var docContent = blob.getDataAsString()
Logger.log("Result TSV: " + JSON.stringify(docContent.split("\t")));
}
Logs:
[15-04-07 14:53:08:767 EDT] Initial TSV: ["time","url","code_name","etcetera"]
[15-04-07 14:53:09:542 EDT] Result TSV: ["time","url","code_name","etcetera"]
Is there a way to concatenate strings and variable values and new lines, in javascript?
My code:
var variab = "Make:"+ make \n
"Model:" + model \n
"Date taken: " + dateTime \n
"File name:" + fileName ;
variab = variab.replace(/\n/g, '<br>');
document.getElementById('exifid').innerHTML = variab;});
make, model, dateTime and fileName are all variables that return specific values, and I want them to be displayed into a list-like form in my div.
You almost got it:
var variab = "Make:" + make + "\n" +
"Model:" + model + "\n" +
"Date taken: " + dateTime + "\n" +
"File name:" + fileName ;
variab = variab.replace(/\n/g, '<br>');
document.getElementById('exifid').innerHTML = variab;
You can also put each line in an Array and use .join("<br>") to save yourself some typing.
Why use the replace?
var variab = "Make:"+ make + "<br>" +
"Model:" + model + "<br>" +
"Date taken: " + dateTime + "<br>" +
"File name:" + fileName ;
document.getElementById('exifid').innerHTML = variab;
You could stuff each element into an array and then join them.
var array = [
"Make:"+ make,
"Model:" + model,
"Date taken: " + dateTime,
"File name:" + fileName
];
document.getElementById('exifid').innerHTML = array.join('<br/>');
For greater readability, I'd do this:
var variab = [
"Make:" + make,
"Model:" + model,
"Date taken: " + dateTime,
"File name:" + fileName
].join("<br />");
You're also free to include newlines within your fields this way.
Unless your needs grow more complex. In that case, I'd use a templating framework.
It is much, much better to abstract the data into a readable form using a JavaScript object, like below, if possible.
var variab = {
"Make": "Audi",
"Model": "A3",
"Date Taken": 2006,
"File Name": "audia3.doc"
}
var string = "";
for (var detail_type in variab){
var detail_data = variab[detail_type];
string += detail_type + ": " + detail_data + "<br/>"
}
document.getElementById('exifid').innerHTML = variab;