Proper handling text that was sent via JSON when returning html string and inserting it into tittle attributeASP.NET MVC controller actions that return JSON or partial htmlHTML-encoding lost when attribute read from input fieldHow to remove line breaks from a file in Java?Selecting all text in HTML text input when clickedProper way to return JSON using node or ExpressGet Attributes of enclosing Tag in HTML StringHow to prevent CKEditor to add a space ( )?How to escape a concatenated string in html title attributeinsert attributes dynamically in html elements in angularHow to prevent html attribute value from generating new line, my json string value breaks

GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?

Im going to France and my passport expires June 19th

How did the Super Star Destroyer Executor get destroyed exactly?

Bullying boss launched a smear campaign and made me unemployable

Avoiding the "not like other girls" trope?

How seriously should I take size and weight limits of hand luggage?

What about the virus in 12 Monkeys?

What is the most common color to indicate the input-field is disabled?

Why do bosons tend to occupy the same state?

How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)

Why doesn't using multiple commands with a || or && conditional work?

Why is consensus so controversial in Britain?

How do I gain back my faith in my PhD degree?

Why would the Red Woman birth a shadow if she worshipped the Lord of the Light?

What mechanic is there to disable a threat instead of killing it?

Are there any examples of a variable being normally distributed that is *not* due to the Central Limit Theorem?

How can saying a song's name be a copyright violation?

How dangerous is XSS?

Avoiding direct proof while writing proof by induction

How does a predictive coding aid in lossless compression?

Plagiarism or not?

Reverse dictionary where values are lists

How could indestructible materials be used in power generation?

What exploit Are these user agents trying to use?



Proper handling text that was sent via JSON when returning html string and inserting it into tittle attribute


ASP.NET MVC controller actions that return JSON or partial htmlHTML-encoding lost when attribute read from input fieldHow to remove line breaks from a file in Java?Selecting all text in HTML text input when clickedProper way to return JSON using node or ExpressGet Attributes of enclosing Tag in HTML StringHow to prevent CKEditor to add a space ( )?How to escape a concatenated string in html title attributeinsert attributes dynamically in html elements in angularHow to prevent html attribute value from generating new line, my json string value breaks













-1















i have the following text (with new lines).



"☢☢☢☢2222def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"

122434 *()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"


I use datatbles.js and there when populating cells i have a function that returns an html string



return '<span title="TEXT"></span>'


When i do this



return '<span title="'+text+'"></span>'


Or this



return '<span title="'+JSON.stringify(text)+'"></span>'


It breaks my returned element.



How could I approach inserting the text above with new lines etc. to the title attribute so it would work properly?



Thank you!










share|improve this question






















  • How does it 'break'? Does the ' character break it?

    – L0uis
    Mar 8 at 22:33






  • 1





    There are things like single and double quotes, square, curly, and angle brackets, and other such symbols and punctuation that can be interpreted as HTML markers instead of plain text. You will need to escape those characters according to this spec.

    – Abion47
    Mar 8 at 22:34












  • @Abion47 Thanks, it helped!

    – Мария
    Mar 10 at 23:16















-1















i have the following text (with new lines).



"☢☢☢☢2222def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"

122434 *()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"


I use datatbles.js and there when populating cells i have a function that returns an html string



return '<span title="TEXT"></span>'


When i do this



return '<span title="'+text+'"></span>'


Or this



return '<span title="'+JSON.stringify(text)+'"></span>'


It breaks my returned element.



How could I approach inserting the text above with new lines etc. to the title attribute so it would work properly?



Thank you!










share|improve this question






















  • How does it 'break'? Does the ' character break it?

    – L0uis
    Mar 8 at 22:33






  • 1





    There are things like single and double quotes, square, curly, and angle brackets, and other such symbols and punctuation that can be interpreted as HTML markers instead of plain text. You will need to escape those characters according to this spec.

    – Abion47
    Mar 8 at 22:34












  • @Abion47 Thanks, it helped!

    – Мария
    Mar 10 at 23:16













-1












-1








-1


1






i have the following text (with new lines).



"☢☢☢☢2222def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"

122434 *()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"


I use datatbles.js and there when populating cells i have a function that returns an html string



return '<span title="TEXT"></span>'


When i do this



return '<span title="'+text+'"></span>'


Or this



return '<span title="'+JSON.stringify(text)+'"></span>'


It breaks my returned element.



How could I approach inserting the text above with new lines etc. to the title attribute so it would work properly?



Thank you!










share|improve this question














i have the following text (with new lines).



"☢☢☢☢2222def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"

122434 *()_+-=[]\|:;"'<>?,./def l2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 1 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./ 2 ❤ ☀ ☆ ☂ ☻ ♞ ☯ ☭ ☢ € → ~`!@#$%^&*()_+-=[]\|:;"'<>?,./"


I use datatbles.js and there when populating cells i have a function that returns an html string



return '<span title="TEXT"></span>'


When i do this



return '<span title="'+text+'"></span>'


Or this



return '<span title="'+JSON.stringify(text)+'"></span>'


It breaks my returned element.



How could I approach inserting the text above with new lines etc. to the title attribute so it would work properly?



Thank you!







javascript json string datatable datatables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 22:31









МарияМария

555




555












  • How does it 'break'? Does the ' character break it?

    – L0uis
    Mar 8 at 22:33






  • 1





    There are things like single and double quotes, square, curly, and angle brackets, and other such symbols and punctuation that can be interpreted as HTML markers instead of plain text. You will need to escape those characters according to this spec.

    – Abion47
    Mar 8 at 22:34












  • @Abion47 Thanks, it helped!

    – Мария
    Mar 10 at 23:16

















  • How does it 'break'? Does the ' character break it?

    – L0uis
    Mar 8 at 22:33






  • 1





    There are things like single and double quotes, square, curly, and angle brackets, and other such symbols and punctuation that can be interpreted as HTML markers instead of plain text. You will need to escape those characters according to this spec.

    – Abion47
    Mar 8 at 22:34












  • @Abion47 Thanks, it helped!

    – Мария
    Mar 10 at 23:16
















How does it 'break'? Does the ' character break it?

– L0uis
Mar 8 at 22:33





How does it 'break'? Does the ' character break it?

– L0uis
Mar 8 at 22:33




1




1





There are things like single and double quotes, square, curly, and angle brackets, and other such symbols and punctuation that can be interpreted as HTML markers instead of plain text. You will need to escape those characters according to this spec.

– Abion47
Mar 8 at 22:34






There are things like single and double quotes, square, curly, and angle brackets, and other such symbols and punctuation that can be interpreted as HTML markers instead of plain text. You will need to escape those characters according to this spec.

– Abion47
Mar 8 at 22:34














@Abion47 Thanks, it helped!

– Мария
Mar 10 at 23:16





@Abion47 Thanks, it helped!

– Мария
Mar 10 at 23:16












1 Answer
1






active

oldest

votes


















-1














If you do string concatenation you have to worry about all the details of encoding in HTML. Instead of passing HTML strings around, create DOM elements with JavaScript functions:



var span = document.createElement("span");
span.innerText = text;
return span;


The caller should then use appendChild() to append this element where it's needed, rather than assigning it to innerHTML.






share|improve this answer

























  • That would just end up in columns containing the text [object HTMLSpanElement]

    – davidkonrad
    Mar 10 at 11:27











  • You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

    – Barmar
    Mar 10 at 11:39












  • I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

    – davidkonrad
    Mar 10 at 13:55











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071915%2fproper-handling-text-that-was-sent-via-json-when-returning-html-string-and-inser%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














If you do string concatenation you have to worry about all the details of encoding in HTML. Instead of passing HTML strings around, create DOM elements with JavaScript functions:



var span = document.createElement("span");
span.innerText = text;
return span;


The caller should then use appendChild() to append this element where it's needed, rather than assigning it to innerHTML.






share|improve this answer

























  • That would just end up in columns containing the text [object HTMLSpanElement]

    – davidkonrad
    Mar 10 at 11:27











  • You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

    – Barmar
    Mar 10 at 11:39












  • I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

    – davidkonrad
    Mar 10 at 13:55















-1














If you do string concatenation you have to worry about all the details of encoding in HTML. Instead of passing HTML strings around, create DOM elements with JavaScript functions:



var span = document.createElement("span");
span.innerText = text;
return span;


The caller should then use appendChild() to append this element where it's needed, rather than assigning it to innerHTML.






share|improve this answer

























  • That would just end up in columns containing the text [object HTMLSpanElement]

    – davidkonrad
    Mar 10 at 11:27











  • You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

    – Barmar
    Mar 10 at 11:39












  • I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

    – davidkonrad
    Mar 10 at 13:55













-1












-1








-1







If you do string concatenation you have to worry about all the details of encoding in HTML. Instead of passing HTML strings around, create DOM elements with JavaScript functions:



var span = document.createElement("span");
span.innerText = text;
return span;


The caller should then use appendChild() to append this element where it's needed, rather than assigning it to innerHTML.






share|improve this answer















If you do string concatenation you have to worry about all the details of encoding in HTML. Instead of passing HTML strings around, create DOM elements with JavaScript functions:



var span = document.createElement("span");
span.innerText = text;
return span;


The caller should then use appendChild() to append this element where it's needed, rather than assigning it to innerHTML.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 10 at 11:40

























answered Mar 8 at 23:37









BarmarBarmar

434k36260363




434k36260363












  • That would just end up in columns containing the text [object HTMLSpanElement]

    – davidkonrad
    Mar 10 at 11:27











  • You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

    – Barmar
    Mar 10 at 11:39












  • I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

    – davidkonrad
    Mar 10 at 13:55

















  • That would just end up in columns containing the text [object HTMLSpanElement]

    – davidkonrad
    Mar 10 at 11:27











  • You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

    – Barmar
    Mar 10 at 11:39












  • I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

    – davidkonrad
    Mar 10 at 13:55
















That would just end up in columns containing the text [object HTMLSpanElement]

– davidkonrad
Mar 10 at 11:27





That would just end up in columns containing the text [object HTMLSpanElement]

– davidkonrad
Mar 10 at 11:27













You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

– Barmar
Mar 10 at 11:39






You don't put the return value in innerHTML, you use appendChild to append it as a DOM element.

– Barmar
Mar 10 at 11:39














I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

– davidkonrad
Mar 10 at 13:55





I understand your point but DataTables expect a string (treats values from the render callback as strings) ...You could use return span.outerHTML but that seem to ruin markup within data / text (i.e if text is <p>something</p>)

– davidkonrad
Mar 10 at 13:55



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071915%2fproper-handling-text-that-was-sent-via-json-when-returning-html-string-and-inser%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme