3

when iam trying to convert javascript object to jquery object like obj = $(obj). The object obj is loosing one of the property values and setting the value as true.if iam using obj[0].Validated its returning the exact values.Please suggest on this.

obj = $(obj);
objValue = obj.attr("Validate");
4
  • what is obj? is it a true JS object or is it a reference to a DOM element? Commented Sep 5, 2011 at 10:03
  • What are you expecting attr("Validate") to return? Are you expecting your JS object to be converted into a DOM object with attributes? Is Validate a property of your custom object or something? Commented Sep 5, 2011 at 10:06
  • expecting attr("Validate") = true/false. converting the JS Object to JQuery Object Commented Sep 5, 2011 at 10:10
  • This question is going nowhere unless you post some code to illustrate your problem. Commented Sep 5, 2011 at 10:12

1 Answer 1

3

Looking at your code, you basically have an array of objects since you mentioned being able to do:

obj[0].Validate

This means that when you convert your object to a jQuery object, you're still dealing with an array of objects.

Simply doing obj.attr('Validate') will faily because you're not accessing a single object in your array yet.

Consider the following:

var x = {obj1 : {Validate: true, SomethingElse: false, AnotherProperty: true}};
var jQx = $(x);

var jQxFirst = $(jQx.attr('obj1'));

Here we can see that I have an collection of objects. In order to check my Validate property I need to access the individual item in the object collection.

This will now work:

console.log(jQxFirst.attr('Validate'));
console.log(jQxFirst.attr('SomethingElse'));
console.log(jQxFirst.attr('AnotherProperty'))

Here's a working example: http://jsfiddle.net/48LWc/

Another example using a more familiar notation to indicate how we're dealing with an array:

http://jsfiddle.net/48LWc/1/

var objCollection = new Array();
objCollection[0] = {Validate: true, SomethingElse: false, AnotherProperty: true};

var jQx = $(objCollection);

var jQxFirst = $(jQx[0]);
Sign up to request clarification or add additional context in comments.

Comments

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.