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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page