jquery how can I use apostrophe is statment - javascript

jQuery('#Level_of_Interest__c option[value="Bachelor's"]').attr("selected","selected")
this is the code I am trying to run but because of the " ' " on Bachelor's I am getting a error, any one has any ideas?

You can use backslash:
//since \ itself is a escape character you need to use \\
jQuery('#Level_of_Interest__c option[value="Bachelor\\'s"]')
.attr("selected","selected")

single backslash is enough.
$('#Level_of_Interest__c option[value="Bachelor\'s"]').attr("selected","selected")

You simply need to escape it using backslash
jQuery('#Level_of_Interest__c option[value="Bachelor\'s"]').attr("selected","selected");

Related

How do I show single and double quotes in a text alert in javascript?

I want a javascript alert that reads, "It's "Hammer" time!"
What is the best code to write this?
Although you could use a string with ' and escape the ', or a string with " and escape the "s, it would be better to use a template literal, which doesn't require escaping of quotes because its delimiter is the backtick:
alert(`"It's "Hammer" time!"`);
For displaying single or double quotes, you can write your code like this alert("\"It's \"Hammer\" time!\"")
You need to escape it using \
For example:
alert("Something \"here\"!!");
Escape the "s other than the ones from the begining and ending..
alert("\"It's \"Hammer\" time!\"")
How to display double quotes in JavaScript
Or use string interpolation, where you can have both without escaping.
alert(`Hello, I'm "nobody"!`);

Check to see if string contains quotes, and if it does, place an escape '\' before

I need to parse strings for single and double quotes, and if the string does contain them, I need to add an escape slash before. What would be the most efficient way? Is there a way to use a regex to check this through a function?
Absolutely!
​var str = 'abcd"\'efg"hij';
alert(str.replace(/(\"|\')/g, '\\$1'));​​​​​​​​​​​​​​​​​​​​​​​​ // alerts abcd\"\'efg\"hi
Could be something like that:
str.replace(/["']/g,"\\$&");
str.replace(/(\"|\')/g, "\\$1");

Bad Character in Regex

i ran into a simple problem, which I think you guys can solve. I am programming javascript, where I use the following code to replace all strings in a string with another string:
str = str.replace(/find/g,”replace”)
Yes, the code works, but what I want to do is:
str = str.replace(/</p>/g,”replace”)
It won't work because of:
</p>.
It dosen't like the /.
Anyone who can help me?
Use an escape:
str.replace(/<\/p>/g, "replace");
^--- escape char.
Escape / with another \. Like <\/p>
Should work.

Using escape function throws js error because special characters are present in text

the sample is like this:-
var encdata= escape('They're good at coding."Super". Its great!');
Now the error comes because it finds the closing apostrophe at they're instead at last.
It will work if i code the same as
var encdata= escape('They re good at coding."Super".Its great!');
Similarly if i use double quotes and give like
var encdata= escape("They're good at coding."Super".Its great!");
It will throw error at "super" but not at they're.
So, it should work when my text contains both double quotes and apostrophe.
And i can't wrap my text within as 'text' or "text".
So, i need an alternate solution for it
Escape the characters with \' or \";
var encdata = escape('They\'re good at coding."Super".Its great!');
var encdata = escape("They're good at coding.\"Super\".Its great!");
you need to use \" or \':
var encdata= escape("They're good at coding.\"Super\".Its great!");
or
var encdata= escape('They\'re good at coding."Super".Its great!');
you have to use a slash \ to escape the apostrophe inside the single quotes, or alternatively the open quotes inside the double quotes.
'They\'re good at coding."Super". Its great!'
"They're good at coding.\"Super\".Its great!"
This is true for almost every language ever. Adding a slash to characters lets it know that you want it to be a literal character instead of having a special meaning.

Escaping quotes from Rails Variables when using them for Javascript?

I am having problems when trying to use a rails variable within javascript code.
For example, I might define a link_to_remote, with parameter
:complete => "alert('my_var');"
If my_var = "I'm testing.", then the javascript code will break due to the single quote closing the code prematurely. If I try using escape_javascript(my_var) so that the quote gets turned into \', it doesn't seem to fix the problem.
I've noticed that when you try alert('I\'m testing'); there's a problem, but if you do alert('I\\'m testing'), it works. Since escape_javascript only turns ' into \', rather than \\', does somebody have a suggestion for how to handle this?
Thanks!
Eric
when you try alert('I\'m testing'); there's a problem
Backslash is also an escape in Ruby strings! So the string literal:
"alert('I\'m testing');"
means the string:
alert('I'm testing');
the backslash is gone already before JavaScript gets a look at it. When you are writing a JavaScript string literal inside a Ruby string literal you need to escape the escape, \\, to get a real \ that will then, in JavaScript, escape the apostrophe.
escape_javascript correctly generates the backslash for JavaScript, if a backslash was included in its input. But again, if you're writing a string literal, you have to escape the backslash to get a real backslash:
escape_javascript("\b") -> this is a backspace character!
escape_javascript("\\b") -> this is backslash-then-letter-b;
escaped for JavaScript literal to double-backslash-then-b.
So, this is fine:
"'"+escape_javascript(myvar)+"'"
alternatively, you can use a JSON encoder to create the JavaScript string literal including the surrounding quotes.

Categories