When to use double quotes/single quotes in parentheses? - javascript

When to use and not use single or double quotes within functions?
for example:
function convertToInteger(str) {
return parseInt(str); // why do we not use double-quotes here? is this as simple as we never use quotes around arguments when calling a function?
}
convertToInteger("56");

the variables inside the function are called arguments. They serve to store the item you passed.
As you passed it, it will use whatever value you enter, but if you put a value in quotes, you would be setting a fixed value.

Variable value should be in quotes , variable name should not be in quotes .
convertToInteger("56");
or
var datavalue="56";
convertToInteger(datavalue);

Related

Escape apostrophe passed from code behind

in ASPX, I call a JS function when clicking on a button, passing in a variable from code behind, something like this:
... onclick="SelectEmpl('<%=employer.Name%>', '<%=employer.Surname%>', '<%=employer.Company%>')"
The problem is, that all parameters passed into the SelectEmpl function are strings and can contain the apostrophe character. In case this happens, the JS apostrophes are paired prematurely and evaluation fails.
I know I need to escape the apostrophes in the strings somehow but I'm not sure how when they are passed from CB.
Thanks
Create a function to escape the apostrophes then call it around each one. Something like:
Shared Function RemoveQuotes(ByVal input As String) As String
Return Replace(input, "'", "\'")
End Function
Then:
... onclick="SelectEmpl('<%=RemoveQuotes(employer.Name)%>', '<%=RemoveQuotes(employer.Surname)%>', '<%=RemoveQuotes(employer.Company)%>')"

how to remove single quote from the value of hidden field in javascript

I am reading hidden field value using javascript. The value I am getting is within single quote( '78963' ). I can i remove this single quote ? I want the value without single quote( 78963 ). Please help me to solve the problem.
I guess you simply want to convert your string value to numeric.
Just use parseInt():
parseInt("78963", 10); // 78963
If the value pretends to be floating, there is parseFloat() method:
parseFloat("78963.1"); // 78963.1
And one more shortcut to make casting:
+"78963"; // 78963
In case if you simply want to replace single quotes, you may use:
"'78963'".replace(/'/g, ""); // "78963"
(as stated by others) or do tricky split:
"'78963'".split("'")[1]; // "78963"
You can use .replace() function on strings: http://www.w3schools.com/jsref/jsref_replace.asp
str.substr(1, str.length - 2);
Just $('#element').val(), you will get exact value. here element will be the id of hidden box.
If it's true that the value of the hidden input field contains single quotes, the (in my opinion) best answer is the one which JamesAllardice put in a comment:
yourValue.replace(/'/g, "");

Convert text box output to be enclosed in single quotes instead of double quotes

When giving string input from a text box to a key of Json variable, In firebug, I am finding that the value is being enclosed by double quotes. Can I avoid this and make that my string to be surrounded by single quotes?
I got this need while using
jqplot, which is used for rendering graphs. In configuring jqpot options, some of the values like options.axesDefaults.tickOptions.mark , etc in jqplot options require values to be enclosed in single quotes. I need to set these options according to user input.
Can any one please provide me solution.
Thank you in advance.
Single quote delimited strings are not valid JSON. It looks like you have a JavaScript object, which isn't JSON.
In JavaScript, there is no difference between single and double quotes.
The example you linked doesn't require single quotes, it just has them there in the example.
Try:
var someVal = "\"test\""
// Global search for " and replace with ' in supplied string
var quoteReplacedVal = someVal.replace(/\"/g,"'");

How can I pass a tag`s value to a javascript function after an event?

How can I pass a tag`s value to a javascript function?
this works:
onclick="submit(this.value)"
but this dosnt work:
onclick="submit(document.getElementById("ShortcutID").value)"
If you want to include quote characters in an attribute value delimited with the same kind of quote characters, you have to represent them with character references.
onclick="submit(document.getElementById("ShortcutID").value)"
Alternatively, use a different kind of quote character.
onclick="submit(document.getElementById('ShortcutID').value)"
… but try to avoid using intrinsic event attributes and bind your JavaScript event handlers with JavaScript instead. See Unobtrusive JavaScript
there is syntax error.
ShortcutID should be enclosed in this 'ShortcutID'
Change this
onclick="submit(document.getElementById("ShortcutID").value)"
to this
onclick="submit(document.getElementById('ShortcutID').value)"
but this dosnt work: onclick="submit(document.getElementById("ShortcutID").value)"
Because you're using double quotes both to delimit the onclick attribute and also inside to delimit the JavaScript string. Try:
onclick="submit(document.getElementById('ShortcutID').value)"
This is why JavaScript allows both single and double quotes for quoting strings.
You can also do it with entities, because remember that the content of an attribute is HTML text just like anything else in the HTML, so:
onclick="submit(document.getElementById("ShortcutID").value)"
The fact that the content is HTML text tends to be problematic when you're doing something non-trivial, which is one reason not to use onclick="code" style event handling but rather hooking up the event in code instead.
The problem you have in your code is you are using two sets of double quotes. To fix this, try this: onclick="submit(document.getElementById('ShortcutID').value)"

String to jQuery.function

What's the simplest way to call a string method with jquery?
If I understand your question correctly, you want to call a method whose name is stored in a string.
If that's the case, you should use square bracket notation instead of (not in addition to) dot notation, and delimit the literal string with quotes:
$["STRINGVALUE"]();
You can also use the variable you defined initially, without quotes:
$[myFunction]();

Categories