Parsing XFN data with Jquery, round 2 - javascript

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_$]*

Related

Cleave.js error in getPostDelimiter() when entering first character into input

While implementing cleave.js for credit card formatting of an input field created as a custom element using litElement I ran into this error when I type the first character into the input but no issue with subsequent characters.
cleave-esm.js:712 Uncaught TypeError: Cannot read property 'slice' of undefined
at Object.getPostDelimiter (cleave-esm.js:712)
at Cleave.onChange (cleave-esm.js:1244)
I tracked it down as far as that it seems getPostDelimiter() is taking a value that is the previous character typed. Since I am looking at the first character this function fails when it tries to execute cleave-esm.js:712.
return value.slice(-delimiter.length) === delimiter ? delimiter : '';
I'm not sure if there is something I can do in my implementation to resolve this or if it's just a bug in Cleave.js that needs to be fixed.
It seems it's an issue introduced in 1.6.0 release.
If you can stick to a previous version, that should get you going.

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>`

Is AngularJS parsing the value incorrectly?

I have an extremely simple example here: http://jsfiddle.net/daylight/hqHSr/
To try it, just go to the fiddle and click the [Add Item] button to add some rows.
Next, click the check box next to any item and you'll see something similar to the following:
Problem: Only Displays Numeric Part
The problem is that the value should display the entire string shown in the row. In the example that means it should display: 86884-LLMUL.
Notice that it only displays the numeric part of the value.
If you look at the control you'll see that I'm using an input of type="text".
Also, if you look at the model (simpleItem) object you'll see that the one property it has is a string.
The JavaScript for the model class looks like:
function simpleItem(id) {
this.id = id;
}
My Attempt To Force to String Type
When I generate each of the simpleItems I even go so far as to set them to a character when I call the constructor (just to force the id to be set to a string type).
Here's the code that initializes each of the simpleItem ids:
currentItem.id = getRandom(100000).toString() + "-" + getRandomLetters(5).toUpperCase();
You can see the rest of the code in the fiddle, but the thing is I generate a random value and concatenate the value together with a hyphen and 5 letters. It's just a silly little piece of code for this sample.
But now, here is the part where it gets really odd.
If I simply change the hyphen - to another character like an uppercase X I get an error each time I click on the checkbox.
Here's the changed code and the new output, which you can see at the revised fiddle: fiddle version 2
currentItem.id = getRandom(100000).toString() + "X" + getRandomLetters(5).toUpperCase();
Also, now if you open Dev Tools in your browser you'll see in the console that Angular is now reporting an error each time you click the [Add Item] button. It looks like:
Adding Single-quotes ?Fixes? It
If you go up to the HTML and alter the following line from this:
ng-init="itemId ={{l.id.toString()}}"
to this
ng-init="itemId ='{{l.id.toString()}}'"
Now when you run it, the error will go away and it will work as you can see at the updated fiddle here: fiddle Version 3
Angular : Converts Hyphen to Minus Sign?
You see, Angular seems to be converting it to a numeric, attempting to do math on it (parsing the hyphen as a minus sign) and then truncating the string portion. That all seems to work when I use a hyphen, but when I use a X then Angular chokes.
Here's what it looks like when you add the single-quotes - of course the angular errors in Dev Tools console go away too.
Angular Forces to Numeric Type?
Why would this occur in Angular? It's as if it is attempting to force my string value to a numeric even though the INPUT element is type text and the JavaScript var is type string.
Anyone have ideas about this?
What About the Asterisk (multiplication symbol)?
Right as I was completing this I wondered what would happen if I changed the - to a * and ran it again. This time I saw the error below, which is indicative that something is attempting to convert to numeric.
This is the expected behavior. Angular is merely interpolating the text you have in your scope into the ng-init expression using scope.$eval and then executing that expression. This has very little to do with what is the type of the input box of the rest of the surrounding context.
It is definitely not desirable that Angular should wrap any interpolation it does in quotes, it'll break its use in all other places such as class="my-class {{dynamic-class}}".
Replace your ng-init with
ng-init="itemId =l.id.toString()"
In following with the docs, you should only use init in special circumstances anyway, you should rely on your controller for this. http://docs.angularjs.org/api/ng.directive:ngInit
I think we're just getting confused with Angular's weirdness. Basically, you're giving angular a string which it's turning into a javascript expression because it's in a {{}}. It's already, explicitly, a string (between the double-quotes):
ng-init="itemId ={{l.id.toString()}}"
It's apparently ignoring the fact that you're saying "hey, no really, this is a real string" with your l.id.toString(). It doesn't care. It's already a string and is going to evaluate it.
Just use the single quotes?
If you ng-init itemId={{undefined===undefined}}, what would you expect to happen? (it prints "true" in the alert).
Same with this: (undefined === undefined is in quotes) ng-init itemId={{'undefined===undefined'}}; prints true in the alert.
ng-init expects an angular expression. You don't have to use curly brackets there. You can simply write it like this:
ng-init="itemId=l.id" ng-click="checkBoxClicked(itemId)"

Why is JSON.parse not working for "23232323."?

I have this field
text = "23232323."
and for validation I want to get it like this 23232323.
I am trying with JSON.parse(text) giving SyntaxError: JSON.parse: missing digits after decimal point
and eval(text) is giving 23232323
How can I fix it?
It can't be parsed because, contrary to popular belief, JSON is a strongly defined language and you can't simply put anything that you could eval.
Here's how numbers are defined :
(see json.org)
You can solve the problem by removing the dot at the end or, better, by not putting it in the first place. Of course, if you trust the source, you can simply use eval('('+yourstring+')') too.

Issue in accessing JSON object?

Am facing issue in accessing the JSON object :
JSON Object am receiving is :
{"71":"Heart XXX","76":"No Heart YYYY"}
I tried to get the value of 71 and 72 separately and use it ...
but am getting some compile time issue as :
Syntax error on token ".71", delete this token
Code:
var map=$("#jsonText").val();
alert(map);
var obj=jQuery.parseJSON(map);
alert("JSON ::"+obj.71);
If am printing obj , am able to view [Object Object]
Can any one out there please help me to find the mistake i did ..I know the question above is asked in many threads in SO . Below are the few threads i found , but failed when i attempted to implement it ..
jquery json parsing
Also tried using the Jquery tutorial given in
Jquery JSON
Its working fine if the key is a String but getting the above error if its a number ...
Try this:
alert("JSON ::" + obj[71]);
"71" isn't a valid property identifier: an identifier should start with a letter, the underscore or the dollar sign. You can avoid this problem using square brackets instead.
Note: everything that's put between square brackets is converted into strings. Even functions, DOM elements or regular expressions: they're all converted with their toString methods, or their superclass' toString.
So 71 there is converted into "71". If you want a little more performance you can directly use the latter. If you don't need it, you can cut some key presses with just 71.
Use instead
alert("JSON ::"+obj["71"]);
according to the rules or javascript a identifier should not start by a number so if it starts by a number or for that matter contains spaces and other special charcters then you should access it by using the [] operator and not by . operator
so obj.71 is invalid but obj["71"] is
try using this site:
http://json.parser.online.fr/

Categories

Resources