Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Javascript object to jquery object?

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");
like image 416
nivas Avatar asked Apr 24 '26 19:04

nivas


1 Answers

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]);
like image 182
Jamie Dixon Avatar answered Apr 27 '26 10:04

Jamie Dixon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!