How to add line break in textContent (JS)? [duplicate] - javascript

This question already has answers here:
How can I insert new line/carriage returns into an element.textContent?
(13 answers)
Closed 4 years ago.
This is a snippet of code, I want to add a line break after each statement. Tried using "/r/n" and "<-br>" none of them seem to work fine.
**else {
para.textContent = num + ' squared is ' + squared(num) + '. ' +
num + ' cubed is ' + cubed(num) + '. ' +
num + ' factorial is ' + factorial(num) + '.';
}**

Instead of textContent you can use innerHTML and <br />s.
para.innerHTML = num + ' squared is ' + squared(num) + '.<br />' +
num + ' cubed is ' + cubed(num) + '.<br />' +
num + ' factorial is ' + factorial(num) + '.';
You can also solve this using CSS. Just add this style white-space: pre; to the target element:
para.setAttribute('style', 'white-space: pre;');

Related

mysql query on javascript gets ER_PARSE_ERROR but it worked in phpmyadmin

var query =
'SELECT '
+ prefix + 'threads.tid as _tid, '
+ prefix + 'threads.fid as _cid, '
+ prefix + 'threads.firstpost as _pid, '
+ prefix + 'threads.views as _viewcount, '
+ prefix + 'threads.subject as _title, '
+ prefix + 'threads.dateline as _timestamp, '
+ prefix + 'threads.sticky as _pinned, '
+ prefix + 'posts.uid as _uid, '
+ prefix + 'posts.tid as _post_tid, '
+ prefix + 'posts.message as _content, '
+ 'IF(filetype like "image%", CONCAT("/uploads/files/MyBBAttachment/", attachname), NULL) as _images, '
+ 'IF(filetype like "image%", GROUP_CONCAT(filename SEPARATOR ","), NULL) as _imgnames, '
+ 'IF(filetype not like "image%", CONCAT("/uploads/files/MyBBAttachment/", attachname), NULL) as _attachments, '
+ 'IF(filetype not like "image%", GROUP_CONCAT(filename SEPARATOR ","), NULL) as _attachnames '
+ 'FROM ' + prefix + 'posts LEFT JOIN ' + prefix + 'attachments ON (' + prefix + 'posts.pid = ' + prefix + 'attachments.pid), ' + prefix + 'threads '
+ 'WHERE ' + prefix + 'threads.firstpost=' + prefix + 'posts.pid '
+ 'GROUP BY ' + prefix + 'posts.pid'
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
Above query doesn't work in Node.js, I get following error:
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0,600000' at line 1
But when I run similar thing in PHPMyAdmin, it works:
SELECT mybb_threads.tid as _tid, mybb_threads.fid as _cid, mybb_threads.firstpost as _pid, mybb_threads.views as _viewcount, mybb_threads.subject as _title, mybb_threads.dateline as _timestamp, mybb_threads.sticky as _pinned, mybb_posts.uid as _uid, mybb_posts.tid as _post_tid, mybb_posts.message as _content
FROM mybb_posts LEFT JOIN mybb_attachments ON mybb_posts.pid = mybb_attachments.pid, mybb_threads
where mybb_threads.firstpost=mybb_posts.pid
group by mybb_posts.pid
LIMIT 0, 100
How do I make var query work?
You're missing a whitespace before the limit keyword:
+ (start >= 0 && limit >= 0 ? ' LIMIT ' + start + ',' + limit : '');
// Here -----------------------^

Putting the variable into the a href tag

i am having issue with assigning the variable into the URL
This is the code
var value = i+1;
var customPopup = 'Latitude: ' + data.Table[i].Latitude + '</br>Longitude: ' + data.Table[i].Longitude
+ '</br>Station: ' + data.Table[i].StationID + ' </br>Box: ' + data.Table[i].BoxID + '</br>Timestamp: ' + data.Table[i].LocationSend + "<br><a target='_blank' href='/Home/History?DeviceID= ' style='color: #000000'>Click Here For Location History</a></br>";
String literals might help (using backticks ``):
var value = i+1;
var customPopup = 'Latitude: ' + data.Table[i].Latitude +
'<br/>Longitude: ' + data.Table[i].Longitude +
'<br/>Station: ' + data.Table[i].StationID +
'<br/>Box: ' + data.Table[i].BoxID +
'<br/>Timestamp: ' + data.Table[i].LocationSend +
`<br/><a target='_blank' href='/Home/History?DeviceID=${value}' style='color: #000000'>Click Here For Location History</a><br/>`;

Why does the first '<br>' show?

I am trying to display strings in Adobe Captivate 9 text captions with JavaScript. I'm using a non-responsive project, and the below code always shows the first "br" tag for newline. If I'm to remove the "br" tags in userResults then the "br" tag in topTenUsers will be seen.
However, in a responsive project, the same code does not show any of the "br" tags.
Code:
var userResults = 'You placed at rank ' + myRank + ' with a score of ' + myScore + '<br>' + '<br>';
var topTenUsers = 'The top ' + numPlayers + ' users are: <br>';
var topTenMsg = userResults + ' ' + topTenMsg;
window.cpAPIInterface.setVariableValue(topTenVarName, topTenMsg);
What is the cause of this difference and how could it be avoided?
Try a self-closing br.
var userResults = 'You placed at rank ' + myRank + ' with a score of ' + myScore + '<br />' + '<br />'

How to use single quotation in string manipulation in javascript

As you can understand in the title, I want use single quotation in string manupulation. Here is my code:
headline += '<article><h5><a class="headline" onmouseover="headLineDetail(' + this.HeadCaption + ',' + this.ShortDescription + ',' + this.PicUrl + ',' + this.NewsId + ')" href="NewsDetail.aspx?nid=' + this.NewsId + '"' + '">' + this.HeadCaption + this.time + '</a></h5>';
I have to give string parameters of headLineDetail with quotation. But I append headline to a div as inner html. How can I use single quotation in this case.
You could use a \' to escape it.
Just escape using a \'
headline += '<article><h5><a class="headline" onmouseover="headLineDetail(\'' + this.HeadCaption + '\',\'' + this.ShortDescription + '\',\'' + this.PicUrl + '\',\'' + this.NewsId + '\')" href="NewsDetail.aspx?nid=' + this.NewsId + '"' + '">' + this.HeadCaption + this.time + '</a></h5>';

Javascript dynamically created div hidden unless document.write() is included

I've created a universal header dynamically that contains the date and time. Currently it acts and looks perfectly fine, but as soon as I remove the document.write('.') it disappears. It seems that I need any sort of write there in order for the dateDiv to appear, the '.' is just a random character used to fill the space.
//write date/time to div
var dateDiv = document.createElement('div');
dateDiv.innerHTML = '<p>' + d_names[curr_day] + ', ' + m_names[curr_month] + ' ' + curr_date + ', ' + curr_year + ' | ' + '<strong>' + curr_hour + ':' + curr_min + ' ' + a_p + '</strong>' + '</p>';
dateDiv.id = 'dateTime';
//dateDiv disappears without a document.write() before being appended to the body. need to fix
document.write('.');
document.body.appendChild(dateDiv);
I haven't been able to find an answer to this yet, anyone see the problem?
As loganfsmyth is implying its likely that your code is executed, when the document is not fully loaded. Try:
window.onload = function(){
//write date/time to div
var dateDiv = document.createElement('div');
dateDiv.innerHTML = '<p>' + d_names[curr_day] + ', ' + m_names[curr_month] + ' ' + curr_date + ', ' + curr_year + ' | ' + '<strong>' + curr_hour + ':' + curr_min + ' ' + a_p + '</strong>' + '</p>';
dateDiv.id = 'dateTime';
document.body.appendChild(dateDiv);
};
Edit:
See for example http://javascript.about.com/library/blonload.htm

Categories