Uncaught Syntax Error: Unexpected Identifier Value (include percent sign) Javascript - javascript

I am trying to assign the value to variable in simple Java Script but getting "unexpected identifier" error, I feel that the% signs are creating problem here.
Any suggestion please?

You need to surround strings with quotes (' or ")

the value you are assigning to the rid is string.
So, always use Quotes
'or"

Related

Adding variables in string JS

I am using a string to store some HTML. However, the string needs to have some variables. The method I am using to input this variables is encountering a problem. There are quotes in my HTML. Therefore the string is cutting short where I don't want it to.
base="<h2>"+data[i]+"</h2><br><button onclick='vote('"+data[i]+'"); return false'>Vote for ME!</button>"
However, I am getting this error.
'vote(' Uncaught SyntaxError: Unexpected end of input
Even if I remove the 2 single quotes in the brackets, I am getting an error.
'vote(Iron Man); return false': Uncaught SyntaxError: missing ) after argument list. NOTE: Iron Man is the value of data[i].
Thanks in advance!
You've got a ' and a " flipped around.
base="<h2>"+data[i]+"</h2><br><button onclick='vote('"+data[i]+'"); return false'>Vote for ME!</button>"
^^---- flip these two
Template literals can make complicated string concatenations much more readable and less error-prone as it would eliminate the need for most of the " double quotes in your code sample:
base = `<h2>${data[i]}</h2><br><button onclick="vote('${data[i]}'); return false">Vote for ME!</button>`

How to make string to json object

I have below string
'[{\'Question\': \'a Names and Roles (if known)\'}]'
I need to convert it into JSON.
I tried JSON.parse(s)
I got error SyntaxError: Unexpected token ' in JSON at position 2
and also
> eval(s)
SyntaxError: Unexpected string
Any help would be really appreciable
Problem is that you need to pass string inside JSON.parse(s).
Please make sure s is a string
'[{*Question\': at the point of asterisk you need a \' don't you?
So it should be
'[{\'Question\': \'a Names and Roles..
EDIT
Found this after a bit of research. Problem is with the single quote. Replace them with double quotes, you are good to go. See this fiddle, to check this out in action.
Additionally, I had to modify "[\'NA\']" in your string, to get it to working. That is also invalid JSON. If it is an array that you want it to be, you should put it like [\"NA\"].

Javascript: how to include a string literal into another one?

Say we've got a tag as follows:
<div ng-init="html='<a ng-click=\'openPopover(\'#ChoixDeLaDensiteRecherche\')\'>click</a>'"></div>
(I actually need to set variables with HTML code)
This doesn't work. How to write escaping for the innermost simple quotes?
PS: I don't know why \" \" didn't worked either.
[EDIT 1] The lecture error:
[Error] Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 12-12 [] in expression [openPopover(].
Try ", example:
<div onclick="alert('<a ng-click="openPopover(\'#ChoixDeLaDensiteRecherche\')">click</a>')">AAAAA</div>
Is there a reason why you're not closing the literal within the parentheses? It seems like you've escaped the closing quote outside the parentheses.
Try this:
<div onclick="alert('<a ng-click="openPopover(\'#ChoixDeLaDensiteRecherche\')">click</a>')">click</div>

Parsing simple JSON using Ext gives SyntaxError: Unexpected token ILLEGAL

i'm doing a simple parse of some JSON and it's giving me an error in chrome, what am i missing here?
Ext.util.JSON.decode("{info: {synopsis: 'test'}}");
SyntaxError: Unexpected token ILLEGAL
Thanks a lot
http://www.json.org/
Think you should use double quotes instead of single quotes.
Ext.util.JSON.decode('{"info": {"synopsis": "test"}}');
Be careful if you are using ExtJs 4 onward, You have to use
Ext.JSON.decode('{"info": {"synopsis": "test"}}');

Parsing XFN data with Jquery, round 2

You guys already helped me on correctly parsing the REL attribute on A tags, but there are two XFN values that I'm not able to match:
"co-worker" and "co-resident". The hyphen causes an error with jquery.
I tried this
xfn_co-worker = $("a[rel~='co-worker']").length;
and this
xfn_co-worker = $("a[rel~='co\-worker']").length;
In both cases the error "Uncaught ReferenceError: Invalid left-hand side in assignment" is returned.
(Being these standard XFN values, I'm forces to use them)
Any idea is appreciated, as usual :-)
This isn't an error in you selector. The error lies in your variable name.
You can't use mathematical operators in the variable name. So the problem is your use of the - sign.
Try replacing
xfn_co-worker
with e.g
xfn_co_worker
And it should work alright
xfn_co_worker = $("a[rel~='co-worker']").length;
Note: Your variable name must match the following regex [a-zA-Z_$][0-9a-zA-Z_$]*

Categories

Resources