views:

196

answers:

3

Getting "Expected ',' or '{' but found '#44559'" error. My code looks like this:

var valueid = $("div#center-box div#empid-textbox input").val(); //valueid=44559
if($("div#esd-names li#" + valueid).length > 0){
   //DO SOMETHING;
};

I'm getting the value of what is entered into a textbox input field which in case is "44559" can't seem to figure out why I'm getting this error.

I call my valueid retrieving function with the following code. After you press ENTER in a specific textbox the value of the textbox is retrieved and ran against list items to see if it exists...if it does -- //DO SOMETHING//

$("div#center-box div#empid-textbox input.id").keypress(function(e){
  key = e.which;
  if(key===13){
    valueid = $("div#center-box div#empid-textbox input").val();
    if($("div#esd-names li[class*='" + valueid + "']").length > 0){
       //DO SOMETHING;
    };
  };
});
+5  A: 

You are using a number as an id. This is not allowed.

kgiannakakis
when I use it as a class instead I get the same error except it says "...but found '0.44559'"
sadmicrowave
@sadmicrowave: That'd be because ".44559" is interpreted as a floating-point number with 0 as the integer point. You'll find the same is true in many programming languages.
Samir Talwar
but the number is just 44559 (no decimal) -- not .44559 -- for some reason firefox is adding a prefix of 0. to my number all on its own. any recommendations?
sadmicrowave
The class selector uses a "." before the class name (i.e. '.class-name'). You're going to need to append a letter or maybe a "_" to the beginning of your number.
climbage
@climbage - I added an "_" to my class-name and this seems to work....can you explain to me why this works though and why I was having a problem in the first place?
sadmicrowave
It's as kgiannakakis said -- an id (or class name) starting with a number is not valid and will cause the parser to read it as a number rather than an ident string.Here's more on variable naming : http://www.codelifter.com/main/tips/tip_020.shtml
climbage
A: 
drusnov
Yes...I preform an action if it does exist (the DO SOMETHING action in my example).
sadmicrowave
you could try either checking if not null:if($("div#esd-names li#" + valueid) != null)or checking if id contains your value with:if($("div#esd-names li[id*=" + valueid + "])
drusnov
neither one of those suggestions worked....
sadmicrowave
I just edited the last example .. it's supposed to be [id*= instead of [id8= ... sorry
drusnov
I figured this much and did [id*= when i tried it...still no worky
sadmicrowave
Actually your syntax is still wrong (missing some quotations you need:...................................... if($("div#esd-names li[id*='" + valueid + "']")....
sadmicrowave
can you post some more code, like your html that holds the elements you are trying to access? where do you call your check, on document-ready or ???
drusnov
I never use quotes and it works fine every time
drusnov
I edited my original question to include more code as you requested.
sadmicrowave
A: 

Put the number in a rel attribute, and check for that $("div#esd-names li[rel=" + valueid + "]")

Aaron Mc Adam
When I do that I get a firefox error saying: "Expected identifier or string for the value in attribute selector but found '44559'"
sadmicrowave