How to add fmt:message into .html() - javascript

My problem is I'm not able to put <fmt:message key="agentIndex.label.renewalBonus" /> inside
$(”#plan1RenewalBonus“).html();
because of the Asterisk (") problem.
I hope it could be
$(”#plan1RenewalBonus“).html("<fmt:message key="agentIndex.label.renewalBonus" />");
anyone have solution for this issue?

Either escape the double quotes within the string (\") or use single quotes.
Also, the quotes in your JQuery selector are invalid. ” != "
Corrected code:
$("#plan1RenewalBonus").html('<fmt:message key="agentIndex.label.renewalBonus" />');

Related

how i can put <script> in double quote

I'm trying to add the script tag using JavaScript. I'm putting the tag into double quote.
My problem is something else but solves with this simple example:
<script>
document.write("<script>" + example_var + "<\/script>");
</script>
How can I fix it?
You can use two ways
Template Literals
document.write(`"<script>"${example_var}"<\/script>"`);
Single Quotes
document.write('"<script>"' + example_var + '"<\/script>"');

How to avoid HTML text from breaking when inserted in input's value?

I would like to replace my hidden input's value with some html text and send it to the server as plain text. Here's how I did :
$('#div').append("<input type="hidden" value='<div><h1>Some text</h1><svg ...></svg></div>'");
The problem is that the code breaks and displays the value of the input as HTML tags into my template, which makes it unusable to the backend when sent.
Here's the jsfiddle: https://jsfiddle.net/Lindow/9p77afc3/2/, while inspecting you'll see that the svg got out of the input's value, that's what I'm trying to avoid.
How can I resolve this issue?
Your input element is not hidden. Provide it with the type="hidden" attribute.
After you added that to your question you have a problem with not escaping quotes. Escape those inner double quotes with backslash, or use single quotes:
$('#div').append("<input type='hidden' value='<div><h1>Some text</h1><svg ...></svg></div>'");
After the next edit, which added the fiddle, it shows you have single quotes in the svg content, and that you do not quote the value property's value.
Do it like this:
$('.elements').append("<div>hey : <input hidden value=\"" + "<svg ... </svg>" + "\"</div>");
// ^^ ^^
Corrected fiddle: https://jsfiddle.net/9p77afc3/4/
Need to change single quotes instead of double quotes.
Try below:
$('#div').append("<input type='hidden' value='<div><h1>Some text</h1><svg ...></svg></div>'");

concatenate variable with background style broken in my case

$("#home").append('<div style="background:url("http://example.com/images/'+obj[i]+'.jpg")"');
what's wrong here? I think I'd closed it properly..
You have issues with mis-matching quotes - you need to escape the double quotes in the url properties' value, or remove them. You have also not closed the div tag properly. Try this:
$("#home").append('<div style="background:url(http://example.com/images/' + obj[i] + '.jpg)"></div>');
Example fiddle
the problem issued with the string you're building:
'<div style="background:url("http://example.com/images/'+obj[i]+'.jpg")"'
let's assume obj[i]==1.
your div will look like this:
<div style="background:url("http://example.com/images/1.jpg")"
notice two important isssues:
the div has no closing ('>' character)
the style attribute is "background:url(" - having same type of quotes prevent the navigator to understand you.
try use:
$("#home").append('<div style="background:url(/'http://example.com/images/'+obj[i]+'.jpg/')">');
good luck!

Jquery, Escaping url with variable

trying to escape and html for appending in jquery with adding a dynamic variable that i am bringing in with ajax and I seem to not be able to get the escaping correct. Here is what I have -
$("<div><div class='presiImg' style='background: url(/\'/gleam\/public\/images\/itPrecedents\/" + keep.logo + "');'></div></div>").appendTo(".myDiv');
I am unsure how to escape this correctly so I can use the variable. Thanks.
You've got a couple issues here:
You're escaping the forward slashes in your URL and that is not necessary
You are using inconsistent quotes in your .appendTo()
As a suggestion, when I append raw HTML using JS/jQuery I try to use the single-quote and the JavaScript quote, and then use the double-quotes in the HTML. For me it is just easier to see that way. Also, the single-quote in the CSS url is not required, and is perhaps confusing the matter.
Anyway, if you change your line to the following it will work:
$('<div><div class="presiImg" style="background: url(\'/gleam/public/images/itPrecedents/' + keep.logo + '\');"></div></div>').appendTo('.myDiv');
There is a runnable example below if you want to see it in action:
$(function() {
var keep = { logo : "test.jpg" };
$('<div><div class="presiImg" style="background: url(\'/gleam/public/images/itPrecedents/' + keep.logo + '\');"></div></div>').appendTo('.myDiv');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="myDiv"></div>
try
$("<div />",{
"class":"presiImg",
"style":"background: url(/gleam/public/images/itPrecedents/"+keep.logo+")"
}).appendTo(".myDiv");

HTML and JavaScript - How to save plain text right

I want to save a locally generated textfile.
This code almost does what I want:
<a download="filename.txt" href="data:text/plain,sometext">Download</a>
sometext represents a string in that tag attribute (inserted using javascript).
You could imagine it as the content of a textarea which is set by the user.
The problem is that sometext must not contain " or ' because otherwise the href attribute thinks that its content finishes earlier which causes the tag to just save a part of that string when you click on it.
As sometext is generated by the user, it can contain these characters and I do not want to force the user to not use these.
Is there another way to save text files using html/javascript?
I do not want to send sometext to the server. (Which would solve the problem because I could create a file there (including " and ') and feature a download link)
It will depend on how are you adding this something to the href.
First of all, a single quote wont matter, because it is wrapped by double quotes ", so it will work well like this:
Download
When it comes to double quotes, in the HTML context you would have to use the correct notation, in this case it would be ". The HTML will understand it as double quotes:
Download
<!-- It will turn into mytext"text -->
However
Since you said that it will be an user's input, I suppose you might be setting the href through Javascript, so I don't think it would matter, it would be like this:
(Try typing single or double quotes... For JS it won't matter)
var ChangeHref = function() {
var el = document.getElementById("a1");
el.href = "data:text/plain," + document.getElementById("inp1").value;
}
<input id="inp1" type="text" />
<input type="button" value="change href" onclick="ChangeHref()" />
<br />
<a id="a1" download="filename.txt" href="data:text/plain,test">Download</a>
Write a simple javascript to remove ' and " characters like so
userText.replace(/\'/g, '').replace(/\"/g, '');
The / means its a regex, the \' and \" represent the literal characters ' and " and the g at the end makes it find all to occurrences to replace.
But since you want to keep it then do this:
userText.replace(/\'/g, '\\\'').replace(/\"/g, '\\\"');
The \ is a literal backslash. that way when you add it to the name of the file it will escape out and mean a literal ' and ".

Categories