0

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?

4
  • please format your code as such, it's more readable that way. Commented Jan 5, 2010 at 9:59
  • Capital N for New should not work. On what browser did it work for you? Commented Jan 5, 2010 at 10:03
  • 1
    Also, in what browser did calling return outside of function scope works? Commented Jan 5, 2010 at 10:05
  • Please provide the most correct working copy you have. It's hard to guess which part of the code is really wrong if there are deliberate or accidental copy/paste errors in it. Commented Jan 5, 2010 at 10:10

3 Answers 3

1

$(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'});

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

Comments

1

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

Comments

0

I think you have to write:

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

or

$(obj).attr('style', 'background:red');

7 Comments

That does not make sense in javascript. Should be: $(obj).css({'background':'red'}). jQuery is a javascript library, the language is STILL javascript.
@slebetman - that broken syntax is in the original question - looks like an inadvertent copy-paste to me.
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
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 .
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.