\n not working in Javascript [duplicate] - javascript

This question already has answers here:
Line break in the mailto onclick
(2 answers)
What is the JavaScript string newline character?
(15 answers)
Closed 8 years ago.
For some reason \n isn't working for me in javascript. Is there anyway to fix it or use html? In my case, I think that using <br> will not work in my href. What are your suggestions.
Working JSFiddle here.
HTML:
<a id = 'emailoff' href = "" target = "_blank">
<div id= "btn1">
<h2 id = "enter">Send Email</h2>
</div>
</a>
Javascript:
$('#btn1').click(function() {
$("#emailoff").attr("href", "mailto:" +
"?subject=Your ThinOptics glasses" +
"&body=To get your new ThinOptics glasses simply click this link and pick the case and color you like best. You'll get free shipping on your order. \n"+
" WWw.Thinoptics.Com/teddy#shalon.com \n" +
"\n Enjoy")
});

You have to URL encode your new lines. The encoded new line should be %0A http://jsfiddle.net/mendesjuan/LSUAh/
<a id = 'emailoff' href = "mailto:me#you.com?subject=hello&body=a%0Ab" target = "_blank">
<div id= "btn1">
<h2 id = "enter">Send Email</h2>
</div>
</a>
If you're doing it in JS, use the following
$('#btn1').click(function() {
$("#emailoff").attr("href", "mailto:" +
"?subject=Your ThinOptics glasses" +
"&body=To get your new ThinOptics glasses simply click this link and pick the case and color you like best. You'll get free shipping on your order. %0A"+
" WWw.Thinoptics.Com/teddy#shalon.com %0A" +
" %0A Enjoy")
});
Note that you don't have to hardcode the URL encoded version, you can use
encodeURIComponent("\n") // %0A
P.S. Wouldn't it be better not to rely on the user's mail client being configured? Use your own server to send the email instead to make sure all users can send a message.

Try
<br>
instead of
\n
\n is rendered as 1 space in html and so is \t or \s

Related

How to get a value of arabic text from input text through change load function jquery

I need to get a value (full names) in Arabic letters from input field so I can run a query using this value. I noticed when I typed in Arabic the javascript code did not work properly and I noticed as well if I make space between the words in English text the javascript code will catch only the first word and the other words will be missed. How can I solve this problem. I looking for the most sample way
HTML Code
<input type="text" name="member" id="member" class="input_field" required />
<div class="modal-body" id="fifth-choice"></div>
Javascript Code
$("#member").change(function() {
$("#fifth-choice").load("menu1.php?member="+$("#member").val());
});
Many thanks
Try to encode the url parameter by encodeURI
$("#member").change(function() {
$("#fifth-choice").load("menu1.php?member="+encodeURI($("#member").val()));
});
If you would like to send more than a value over the same url using jquery load function you can use the below code
$("#myDiv").change(function() {
$("#fifth-choice").load("menu1.php?member=" + encodeURI($("#member").val()) + "&id=" + encodeURI($("#id").val()) + "&doc=" + encodeURI($("#doc").val()));
});
I hope this could help

find and append string [duplicate]

This question already has answers here:
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 6 years ago.
Edit: the proposed question/answer does not solve my problem: Let me rephrase: find the string between <img src="and " />" and replace it with the original plus another string like ?w=500. I thought this might be doable with regex, but I'd be happy to do it in any JS way also. I don't have jQuery in this context though....
Let's say I have a string containing some markup with image tags, among other things, like this:
<img src="supercool.jpg" />
<p>very cool</p>
<img src="mega.jpg" />
How can I, with regex or otherwise, append a given string (say ?w=500) to each src attribute, so that I end up with
<img src="supercool.jpg?w=500" />
<p>very cool</p>
<img src="mega.jpg?w=500" />
I've looked at similar questions on SO but haven't been able to devise a solution, my regex skills are just too poor:)
I am sharing some PHP code using string replace may be this can help you.
Take all your code in a variable with single quotes and than replace jpg with .jpg?w=500. and set header with plain text.
echo str_replace(".jpg",".jpg?w=500",$a);
RegEx has no understanding of elements or attributes, so the following regex is highly fragile. It only looks for src="" and appends a given string to whatever is between the quotes. For a one-off script this should be enough. For anything more sophisticated use a proper HTML parser like SAX or DOM.
var in = '<img src="asd.png" /> <img src="ddd.jpeg" />';
var out = in.replace(/src=\"(.*?)\"/g, "src=\"$1?w=500\"");
out:
<img src="asd.png?w=500" /> <img src="ddd.jpeg?w=500" />
In case you're trying to do this in a browser (you didn't specify), you want something like this (jQuery):
$("img[src]").each(function() {
this.src = this.src + "?w=500";
});
Based in this #Gumbo answer and assuming the string you are giving, that img tag doesn't have any extra attribute before src you can apply this RegEx.
let str = '<img src="supercool.jpg" /><p>very cool</p><img src="mega.jpg" />';
let res = str.replace(/<img src="(?:[^"\/]*\/)*([^"]+)"/g, '<img src="$1?w=500"');
console.log(res);
If you don't need any extra considerations your question looks more like a duplicate of the one I linked to you.
For very simple cases you can use a reg exp to match basic HTML, but once it gets complex, reg exp are a bad idea. Sometimes you need to clean up some code and it works fine.
With your case, your html structure is simple so you can make a match.
var txt = document.getElementById("in").value;
var result = txt.replace(/(<img.*\ssrc=['"])([^'"]+)/g, function(m, l, s){
return m + (s.indexOf("?")!=-1 ? "&" : "?") + "w=500";
});
document.getElementById("out").value = result;
<textarea id="in" rows="4" cols="50">
<img src="supercool.jpg" />
<p>very cool</p>
<img src="mega.jpg?foo=bar" />
<img alt="boo" src="mega.jpg" />
</textarea>
<textarea id="out" rows="4" cols="50"></textarea>
but it can be broken very easy.... You are better off creating a DOM fragment, working with the DOM and changing the attributes. The problem with the DOM solution is it will try to load the images, which is what I think you are trying to avoid in the first place.

Creating functions containing HTML

I'm trying to create a Coffescript function that contains common HTML for a frequently re-used object in my page. I'm passing a variable to the function with the text I want changed each time. Every time I try to compile my Coffeescript, I get this error:
[stdin]:6:5: error: unexpected identifier
<p>"text1"</p>
^^^^^
Here's my code
text1 = "Some text"
ballon1 = (text1) ->
"Open Modal
<blockquote class=\"balloon\" id=\"balloon1\">
<p>"text1"</p>
X
</blockquote>"
I was hoping the output would be:
Open Modal
<blockquote class="balloon" id="balloon1\">
<p>Some text</p>
X
</blockquote>
Any thoughts? I was trying to find the language for the job; maybe I should be using PHP instead? Also, I'm using Javascript because I thought the code needed to be run client-side, since I want to pass different text to the function depending on what links are clicked and when.
Since this is CoffeeScript, you can use string interpolation:
ballon1 = (text1) ->
"Open Modal
<blockquote class=\"balloon\" id=\"balloon1\">
<p>#{text1}</p>
X
</blockquote>"
You could also switch to single quotes in your HTML to avoid all the backslashes:
ballon1 = (text1) ->
"<a href='#balloon1'>Open Modal</a>
<blockquote class='balloon' id='balloon1'>
<p>#{text1}</p>
<a href='#close' title='Close' class='close'>X</a>
</blockquote>"
Or, if you're like me a think single quotes look funny in HTML, you could use a block string for your HTML snippet:
ballon1 = (text1) ->
"""
Open Modal
<blockquote class="balloon" id="balloon1">
<p>#{text1}</p>
X
</blockquote>
"""
A block string even lets you nicely indent the HTML for readability. This is the version I'd probably go with.
If you want string concatenation, you want the + operator:
ballon1 = (text1) ->
"Open Modal
<blockquote class=\"balloon\" id=\"balloon1\">
<p>" + text1 + "</p>
X
</blockquote>"
Change is on the fourth line.
That said, you might consider looking into templating libraries if this is something that comes up a lot. That way (with many of them) you can author your templates in an HTML editor, embedding them in your page, and not have to fuss about with quote character escaping.

how to user Json Value in anchor tag data attribute?

I have an anchor tag which is used to store 'Search-type' in its data-search_type attribute.
on anchor tag click this value is used by $.post ajax method to search for data based on the numbers,
But if you look properly the because of ""(double quotes) in json the data-search_criteria stores "[" as value and ignores the rest of the data.
so my question is what to do to make this data available to $.post method?
<a class="showresult" data-search_type = "Search by Cost" data-search_criteria="["0","1"]" href="#">Open Search</a>
Replace the containing double quotes " with single quotes '
<a class="showresult" data-search_type = "Search by Cost" data-search_criteria='["0","1"]' href="#">Open Search</a>
Use " to represent a " character in an attribute value delimited by " characters.

how to replace particular text in div with some html using jquery? [duplicate]

This question already has answers here:
jQuery if div contains this text, replace that part of the text
(5 answers)
Closed 8 years ago.
Basically what i am looking to do is make this HTML
<div id="_EmailBody">
<p>Hi *|FirstName|*</p>
<p>My name is B and I am the Account Executive.
*|FirstName|* *|LastName|*</p>
</div>
Into this using jquery, when the page loads for display
<div id="_EmailBody">
<p>Hi <span class="meta-tag">FirstName</span></p>
<p>
My name is B and I am the Account Executive.
<span class="meta-tag">FirstName</span> <span class="meta-tag">LastName</span>
</p>
</div>
So replace each *| with <span class="meta-tag">
and each |* with </span>
What is the best way to achieve this using jquery?
This should do it:
var emailBody = $('#_EmailBody').html();
emailBody = emailBody.replace(/(\*\|)/g, '<span class="meta-tag">');
emailBody = emailBody.replace(/(\|\*)/g, '</span>');
jQuery('#_EmailBody').html(emailBody);
Use jQuery's built in "html" method to get and set the html. Use regex replace to get your desired html from the string.

Categories