I'm trying to set a button value to be « (or ») via JavaScript with the following code:
document.getElementById('hideButton').value='«';
This just sets the button text to « rather than «.
The HTML markup works fine (i.e. <input type="button" value="«">) giving the double left arrow quote on the button face.
Is there some special way of escaping the ampersand code in JavaScript?
Thanks,
FM
here just convert to UTF it should work.
document.getElementById('hideButton').value='\u00AB';
document.getElementById('otherButton').value='\u00BB';
here a link to convert special text
couldn't you just do this?
document.getElementById("hideButton").value = "«";
Sorry I would have just commented, but haven't got enough rep yet.
Alternatively, use the unicode \u00AB
http://jsfiddle.net/s53FH/
you could use the -element instead
<button id="hideButton"> </button>
<script>
document.getElementById('hideButton').innerHTML='«';
</script>
http://jsfiddle.net/6m3zz/
Related
Description:
I have a very simple button that I want to call a javascript function when clicked. Everything works fine with the function and the button with the exception of one issue that I can't seem to overcome.
The Javascript function copies a string in to the clipboard to be easily pasted for the user. The string is pulled in from another area that is free text for the user to put in what ever they want. However, if the user adds a double quote, it escapes the parent string.
Example:
Example of working code button:
<button id="Demo" onclick="copytoclipboard('some text to copy goes here')">Copy to Clipboard</button>
Example of when the button will not work:
<button id="Demo" onclick="copytoclipboard('some "text" to copy goes here')">Copy to Clipboard</button>
As you may be able to see, the first double quotation mark is ending the parent string.
What I have tried:
So far I have tried escaping the double quote but this doesn't appear to work due to the fact that it is a child string.
Example:
<button id="Demo" onclick="copytoclipboard('some \"text\" to copy goes here')">Copy to Clipboard</button>
Back to the question:
How do I prevent any double quotes in the child string the ending the parent string that is in turn preventing my onclick function to run?
This breaks down to: you're looking to escape data (doesn't matter whether plain text or javascript) in an XML (or more specifically HTML) attribute value.
You'll find plenty of info about that - in this case the easiest way is to escape the doublequote char (") via "
<button onclick="alert("foobar");">Click me</button>
The way that you tried escaping the double quote was almost perfect but you need to do it like this:
<button id="Demo" onclick='copytoclipboard("some \"text\" to copy goes here")'>Copy to Clipboard</button>
You can learn more about escaping special characters in here.
I'd like to put a unicode up arrow in an html input button that is being generated in Javascript.
These are the codes for the up arrow from unicode-table.com:
Unicode number: U+2191
HTML-code: & #8593; (space between the & and # so you can see the code and not the arrow)
I do have
<charset="utf-8" />
in my head tag in the html file.
I've tried every variation of those two codes I can think of within the <> below and all I get in the browser is the text of the codes I've tried.
Here's the line of code:
upButton.setAttribute("value", "<up arrow code here>");
try this string instead of an html escape character (this is a JavaScript string after all, not an html text node) \u2191'
To escape any symbol in JavaScript, use this tool: http://mothereff.in/js-escapes#1%E2%86%91 For your example, '↑' becomes '\u2191'.
To escape any symbol in HTML, use this tool: http://mothereff.in/html-entities#%E2%86%91 For your example, ↑ becomes ↑ or ↑.
Try using this in your html: ↑
can you please tell me how to remove the span tag using jquery.
INPUT
<span class="abc">PQR </span>
OUTPUT
PQR
I done before but don't remember I think I used regex or replace .:(
Second
How to replace   by a space(" ") ?
I used like that but not work.
replace(/ /g,'');
For the first part, Tim's answer is great. $('.abd').contents().unwrap().
For the second part, please do it properly. is an html-encoded special characters (non-breakable space), but there are tons of other characters you can encounter (like é for é).
Unfortunately there's no built-in standard html decode function, but you can create one with jquery like this:
function htmlDecode(encodedText) {
return $("<div>").html(encodedText).text();
}
For the first part, you can use $('.abc').contents().unwrap()
For the second part see jods answer :)
I am trying to turn a <div> into a link to local HTML document (./lilo/index.html) using JavaScript.
HTML
<div class="pagelist_item" onClick="goto("./lilo")">
<h4>Test Button</h4>
<h6>Discription</h6>
</div>
JavaScript
function goto(url){
window.location = url;
alert(url);
}
See http://jsfiddle.net/6HHTd/
But when I click the button, nothing happens.
Why does this not work?
Your quotes are incorrect in this line:
<div class="pagelist_item" onClick="goto("./lilo")">
jsfiddle even shows the error in red text.
Using apostrophes makes it easier to fix:
<div class="pagelist_item" onClick="goto('./lilo')">
To clarify, in "hi "there" you" the second double-quote matches with the first, closing the string and causing an error with the rest of the expression. Escaping the quotes with back-slashes works "hi \"there\" you" but embedding apostrophes (single-quotes) within double-quotes is often easier. (JavaScript is happy to use either single or double-quotes to delimit strings.)
Also rename your function from goto, as it is a reserved keyword.
Use jquery as follows
$('.pagelist_item').click(function(){
window.location="./lilo";
});
Fiddle
I am not finding a solution on this one using JavaScript (to utilize localStorage) in a JSP.
Trying to pass something with apostrophe. I have done a .replaceAll() and replaced the ' with ' and it still passes it as an '.
I have also tried a .split("'") and replaced the apostrophe with:
(\' , ' , \', '' , ''' and '\'')
All of these just pass an apostrophe to the function (what I see when I hover over the link) like this:
Save job
With a and b being the two split substrings but with no effect. I do notice that spaces are converted into %20, but that's little comfort. Any other ideas?
Your JSP code is irrelevant. Decide what HTML you want to produce and produce it.
The following are all valid HTML markup:
<a href="saveJob('Bob\'s Question')"> …
<a href="saveJob("Bob's Question")"> …
<a href="saveJob('He said "Go Away"')"> …
<a href='saveJob("He said \"Go Away\"")"> …
… and the following are invalid:
<a href="saveJob('Bob's Question')"> <!-- JS string ends early -->
<a href="saveJob("Bob's Question")"> <!-- HTML attribute ends early -->
<a href="saveJob('Bob's Question')"> <!-- JS string ends early -->
<a href="saveJob('He said "Go Away"')"> <!-- HTML attribute ends early -->
You cannot use your HTML attribute delimiter in your attribute value except as an HTML entity. You cannot use your JavaScript string delimiter in your JavaScript string unless you escape it, even if you use an HTML entity to describe it.
In general, you should not be putting JavaScript in your HTML (you should attach event handlers to your markup programmatically, from script), and you especially shouldn't be abusing an HTML anchor as a JavaScript trigger (either use an HTML anchor to a valid URL and let JavaScript hijack the link if enabled, or use a <button> or other element to invoke script-only side effects).
As you've noticed, such manual string escape tasks can be quite tricky; covering apostrophes won't even get you all the way: what if there's a newline in the string? That would break the script as well.
I would recommend converting your data to a JSON object, perhaps using JSON-taglib. This should take care of all required escaping for you.
The Phrogz solution
<a href="saveJob("Bob's Question")">
works fine if you have only apostrophes in your text.
If your text contains both apostrophes and quotes, you can use a hidden div (div with style='display:none;') for the text, pass the id of the div to saveJob instead of passing the text itself, and get the text inside saveJob by using
document.getElementById(myId).innerHTML