I'm trying to escape quotes in the text I need to change.
The problem is that if I have quotes in #stuff (" or ' in CHANGE1 or CHANGE2) it keeps failing my whole request. Otherwise if I escape it with escapeQuotes() it won't find " in my original text.
(function($) {
$.fn.escapeQuotes = function() {
return this.html().replace(/"/g, '"');
}
})(jQuery);
$("#stuff").each(function() {
$(this).html($(this).html().replace("CHANGE1", "CHANGE2")).escapeQuotes();
});
Let's say I have this in #stuff:
<div id="stuff">
I'm trying to escape this. ALSO I'D LIKE TO ESCAPE THIS: ".
</div>
Related
Can't find the answer, I tried with what I found on this website and elsewhere but it never works.
Goal : to trim white spaces before and after a word giving in the field DIV.
Problem : everything works fine as long there is NO white space before a word ! There can already be text in the DIV of it can be an empty DIV where text can be inserted.
I tried in the javascript in the DIV like trim(this) but that doesn't work. Even inthe script itself I tried $.trim(editableObj.innerHTML) , nor editableObj.innerHTML.trim()
Nothing works !
JS
<script>
function showEdit(editableObj) {
$(editableObj).css("background", "#FFF");
}
function saveToDatabase(editableObj, column, linkmainid, nav, linkid) {
$(editableObj).css("background", "#FFF url(loaderIcon.gif) no-repeat right");
$.ajax({
url: 'save'+nav+'.php',
type: "POST",
data: 'linkcat=' + column + '&linktitle=' + editableObj.innerHTML + '&linkmainid=' + linkmainid + '&action=' + nav + '&linkid=' + linkid,
success: function(data) {
$(editableObj).css("background", "#FDFDFD");
window.location="/website/cms/menu.php";
}
});
}
HTML
<div class="navnew" contenteditable="true" onBlur="saveToDatabase(this,'1','999999','navnew','111')" onClick="showEdit(this);"></div>
var stringWithLeadingSpaces = " hello world!"
var trimmedString = stringWithLeadingSpaces.trim();
console.log(trimmedString); //logs "hello world!"
trim
Add this snippet to your code:
String.prototype.trim = String.prototype.trim || function trim() {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };
Then, use:
var myValue = " one, two, three ".trim();
or
var myValue = " one, two, three ";
var myValueTrimmed = myValue.trim();
Typing spaces into the content editable DIV inserts non breaking space characters. If you retrieve the content of editableObj.innerHTML these show up as sequences of the character entity string " ". Naturally String.prototype.trim does not remove occurrences of " " because they are not white space text!
If you retrieve the content as editableObj.textContent the text returned contains actual non-break spaces with character code 0xA0. String.prototype.trim does remove these.
Ultimately choose a trim method that suits your needs, depending on whether you want to save HTML or plain text in the database.
I am trying to replace quote (') with \' so as to escape escape quote or double quote in string
<ul id="list">
</ul>
<button id="sethrefbtn" onclick="setlink();">Set Link</button>
function setlink(){
var data = {
playerID : 102458,
playername: "Real Madrid's cristiano Ronalado"
}
listring= "<li><a href='SearchServlet?q=" + data.playername.replace(/'/g, "\'"); + "&playerid=" + data.playerID + "&page=1#pg0'>"+ data.playername +"</a></li>";
$("#list").append(listring);
}
Here is fiddle: fiddle
The desired output should be:
Real Madrid's cristiano Ronalado
Your problem is being caused by it being in an HTML attribute (so you need to convert it to an HTML character reference and not escape it). You should deal with that by using DOM instead of string mashing to build your HTML.
However, in this particular case, you are putting it in a URL, so you should be escaping it for URLs first.
You can do that with encodeURIComponent, but since you are building an entire query string and are using jQuery, you can use param instead.
function setlink() {
var data = {
playerid: 102458,
q: "Real Madrid's cristiano Ronalado",
page: 1
}
var url = "SearchServlet?" + $.param(data);
var li = $("<li />").append(
$("<a />").text(data.q).attr('href', url)
);
$("#list").append(li);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="list">
</ul>
<button id="sethrefbtn" onclick="setlink();">Set Link</button>
listring= "<li><a href='SearchServlet?q=" + data.playername.replace(/'/g, "\'") + "&playerid=" + data.playerID + "&page=1#pg0'>"+ data.playername +"</a></li>";
Just replace the above statement in the code .
There is a semicolon in the middle of the statement in your code.
I'm trying to fix an inline javascript snippet with reg ex to be rewritten on an input field.
I have single quotes that wrap the inner double quotes and I want to convert the inner quotes to be single instead of double but I only want to convert them when the code appears as nested quotes.
Right now I'm doing the conversion for all instances of the quote which works but I might run into another problem. I'll attach my code below.
JavaScript
$(document).ready(function() {
$('.submit').bind('click', function(e) {
var yourstring = $("textarea").val();
yourstring = yourstring.replace(/'/g, "''");
yourstring = yourstring.replace(/"e;/g, "\"");
yourstring = yourstring.replace(/&lquote;/g, "\"");
yourstring = yourstring.replace(/&rquote;/g, "\"");
yourstring = yourstring.replace(/"/g, "\"");
yourstring = yourstring.replace(/“/g, "\"");
yourstring = yourstring.replace(/”/g, "\"");
yourstring = yourstring.replace(/"/g, "\"");
alert(yourstring)
});
});
HTML
<textarea style="width:400px;height:300px;">
function('');
<a onclick="$(".text").append('test');">Click Me</a>
<br/>
&lquote;test quotes&rquote;
" generic quotes "
</textarea>
<input type="submit" class="submit">
To be more specific I'm trying to get
<a onclick="$(".text").append('test');">Click Me</a>
To look like
<a onclick="$('.text').append('test');">Click Me</a>
The example provided does not seem to have anything to do with the database but a problem with the JavaScript code within the onclick attribute. That code actually doesn't have a valid syntax, but my guess is that you are trying to build some sort of editor for HTML code.
If you decide not to follow the recommendations in the comments above (which I agree with), to answer your question, here's an option for removing double quotes for text already surrounded by them:
var s = "<a onclick=\"$(\".text\").append('test');\">Click Me</a>";
var re = /"(.*)"/g;
var r = s.replace(re, function($0, $1) { return '"' + $1.replace(/"/g, "''") + '"'; });
console.log(r);
How do you escape characters when including a variable in a href, img attributes etc. This does'nt seem to work:
var myData = JSON.parse(jsonString2);
var $grouplist = $('#groups');
$.each(myData, function() {
$('<li><a href="'+ this.url + '"/><img src="'+ this.src + '" class="image0"/></a></li>').appendTo($grouplist);
});
}
Whole function above.
$('<li><a href=\"' + this.url + '\"</a></li>').appendTo($grouplist);
You don't need to. Create the element and set the attribute, then you don't need to worry about escaping anything, neither the quotation marks, not the attribute value:
$('<li>').append($('<a>', { href: this.url })).appendTo($grouplist);
Edit:
The code with the image element also:
$('<li>').append(
$('<a>', { href: this.url }).append(
$('<img>', { src: this.src, 'class': 'image0' })
)
).appendTo($grouplist);
You only need to escape when you have the same quotes. In your case this will work:
$('<li><a href="' + this.url + '"</a></li>').appendTo($grouplist);
Because the double quotes won't interfere with the single ones.
You don't need to escape the double quotes, because they are inside single quotes. Also, you didn't finish the opening a tag.
Check out window.escape. There is also encodeURIComponent, which is generally preferred if it is user-input.
As others pointed out, you don't need to escape your double quotes there. So try this:
$('<li></li>').appendTo($grouplist);
Or Guffa's approach:
$('<li>').append($('<a>', { href: encodeURIComponent(this.url) })).appendTo($grouplist);
I need to pass a variable to a JavaScript function, but I have a little trouble. In the .cs file, I have written:
string id = "some'id";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
As you can see there is an ' (single quote) in the id. Is there any way to work around this issue?
Escape ' with a \ (backslash). For example,
console.log('string with \'');
Escape your string for such kind of characters"/","\","'"
example
string id = "some/'id";
You should escape your string , which would lead to :
id = "some\'id";
<script type="text/javascript">
function myFunction(someid) {
someid = someid.replace('#', '\'');
alert(someid);
}
</script>
in Your code
string id = "some'id".Replace("'","#");
this.Controls.Add(new LiteralControl("<input type=\"button\" value=\"Test\" onclick=\"myFunction('" + id + "');\">"));
Hope this will Helps you..