1

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?

5
  • Are you sure that parameter contains this value? First, the object is malformed (but maybe you just forgot a ') and second, it seems unlikely to me that .find('[$id=hfurl]') returns such an object (if you are using jQuery). Do you get any errors on the console? Commented Oct 22, 2010 at 10:33
  • I HAVE EDITED MY QUESTION NOW CHECK OUT... Commented Oct 22, 2010 at 10:37
  • "{'objType':'100','objID':'226','prevVoting':'" is still not correct syntax. What about errors? $(this).find('[$id=hfurl]') likely returns a jQuery object. Are you using jQuery? Commented Oct 22, 2010 at 10:38
  • THIS IS CORRECT ACTUALLY I AM STORING {'objType':'100','objID':'226','prevVoting':' IN HIDDEN FIELD VALUE AND ON CLIENT SIDE I AM RETRIVING IT THATS ALL... Commented Oct 22, 2010 at 10:40
  • It would help us very much, if you'd answer my questions: Are you getting errors on the console? and Are you using jQuery? Commented Oct 22, 2010 at 10:49

2 Answers 2

1

Try using parameter['objType'].

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

Sign up to request clarification or add additional context in comments.

2 Comments

Assuming everything else is correct, if parameter['objType'] works, then parameter.objType should work too.
Your updated code changes things a bit :) Felix has just posted a perfect answer to you question, then
1

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.

2 Comments

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: 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.