tags:

views:

49

answers:

2

Hi i am using a Java script variable

var parameter = $(this).find('#[id$=hfUrl]').val();

this value return to parameter now

"{'objType':'100','objID':'226','prevVoting':'"   // THIS VALUE RETURN BY 

$(this).find('[$id=hfurl]').val();

i want to store objType value in new:

 var OBJECTTYPE = //WHAT SHOULD I WRITE so OBJECTTYPE contain 400

i am trying

OBJECTTYPE = parameter.objType; // but it's not working...

what should i do

+1  A: 

Try using parameter['objType'].

Just a note: your code snippet doesn't look right, but I guess you just posted it wrong.

abahgat
Assuming everything else is correct, if `parameter['objType']` works, then `parameter.objType` should work too.
Felix Kling
I HAVE EDITED MY QUESTION YOU CAN CHECK............
Nishant
NO parameter['objType'] IT'S NOT WORKING....
Nishant
Your updated code changes things a bit :) Felix has just posted a perfect answer to you question, then
abahgat
+1  A: 

Ok, not sure if I am correct but lets see:

You say you are storing {'objType':'100','objID':'226','prevVoting':' as string in a hidden field. The string is not a correct JSON string. It should look like this:

{"objType":100,"objID":226,"prevVoting":""}

You have to use double-quotes for strings inside a JSON object. For more information, see http://json.org/

Now, I think with $(this).find('[$id=hfurl]'); you want to retrieve that value. It looks like you are trying to find an element with ID hfurl,but $id is not a valid HTML attribute. This seems like very wrong jQuery to me. Try this instead:

var parameter = $('#hfurl').val();

parameter will contain a JSON string, so you have to parse it before you can access the values:

parameter = $.parseJSON(parameter);

Then you should be able to access the data with parameter.objType.

Update:

I would not store "broken" JSON in the field. Store the string similar to the one I shoed above and if you want to add values you can do it after parsing like so:

parameter.vote = vote;
parameter.myvote = vote;

It is less error prone.

Felix Kling
actually u r correct "{'objType':'100','objID':'226','prevVoting':'" what i am doing here am adding some another value to it like parameter++ vote + "','myvote':'" + vote + "'}",
Nishant
@Nishant: Ok, that does not matter, but the strings *inside* the object must be enclosed in double-quotes. Could you please edit your question and add the corresponding line where you are adding the other string?
Felix Kling