views:

67

answers:

3

After creating an object with JavaScript I am looking to access it with jQuery to manipulate it. Here is some code to make things clearer:

ObjectFunction = function(content) {
  this.content = content
}

ObjectFunction.prototype = {
   showobject : { return this.appendTo(document.body)}
}

obj = New ObjectFunction()

What I would like to do is as follows, but the syntax is wrong:

$(obj).css{(background:'red')}

This doesn't work. If I create a function in prototype, which would look something like this, it works:

ObjectFunction.prototype = {
 showobject :  { return this.appendTo(document.body)},
 objectmodify: { return this.css{(background:'red')}}
}

// then call something like 
obj.objectmodify()

Is there any way to avoid this ugly code for a direct jQuery function applied on the object?

A: 

I think you have to write:

$(obj).css("background","red");

or

$(obj).attr('style', 'background:red');
Newbie
That does not make sense in javascript. Should be: `$(obj).css({'background':'red'})`. jQuery is a javascript library, the language is STILL javascript.
slebetman
@slebetman - that broken syntax is in the original question - looks like an inadvertent copy-paste to me.
Dominic Rodger
i tried that but it didnt work. if i run $(obj).size() the result is 1 but somehow i dont see the effect on the object
salmane
the code is not an exact copy of what i have so the capital letters and other obvious mistakes can be ignored. the code works fine except for the jquery call to the object .
salmane
Yeah, this question is borken somehow (note the bork). Applying DOM manipulations, which is what jQuery essentially wraps, to a non-DOM object doesn't make a shred of sense. I suspect there is a deeper question about the understanding of javascript that the OP should really be asking. Perhaps the function should return a DOM object but the OP doesn't know how to do it?
slebetman
the object constuctor does create a dom object perhaps there is a way to connect the two but i assumed that if an object has a method that creates a dom object this dom object is linked to the javascript object. It sounds like this isnt the case. How would I go about doing that then?thanks :)
salmane
I figured out the answer. as always it was simpler than i thoughtthe dom element created is referenced as a property of the javascript object. which can be accessed in this case ( see the code above ) by obj.contentso in the is case $(obj.content).css({background:'red'}). works :)I hope this helps someone out there :)
salmane
+1  A: 

$(obj).css{(background:'red')}

The syntax of this is wrong, you have the parenthesis and squigly's the wrong way round - it should be

$(obj).css({background:'red'});

Jamiec
+1  A: 

I figured out the answer. as always it was simpler than i thought.

the dom element created is referenced as a property of the javascript object. which can be accessed in this case ( see the code above ) by obj.content so in the is case $(obj.content).css({background:'red'}). works :) I hope this helps someone out there :)

salmane