2

I have an object that should be a valid jQuery object. When I look at the variable in FireBug it contains all the jQuery functions I'd expect (clone, remove, removeat, etc.). However, I don't see html() as a valid function and when I do this:

stringValue = myjQueryObject.html();

It does fail, saying html() is not a function. However if I do something like this:

stringValue = myjQueryObject[0].innerHTML;

It will correctly pass back the object, minus the parent div and text (which I would expect, seeing as how it is just getting the innerHtml). What do am I missing here?

As noted below, it was an external library that was generating myjQueryObject that had previously returning a valid jQuery object and was updated ... incorrectly. For posterity's sake, I've updated my unit tests to verify that the external library returns a correct jQuery object, make sure this doesn't return null or undefined:

myjQueryObject.jquery

Thanks all! Had a bit of a freakout when my code suddenly broke this morning.

5
  • sounds pretty unlikely since jQuerys .html() also uses the .innerHTML property. You should provide the defination of myjQueryObject plus your markup. jsfiddle.net/4yUqL/14 Commented Dec 8, 2010 at 16:45
  • I don't think "myjQueryObject" is a jQuery object: jQuery objects do have html() but they don't have innerHtml() method. How are you creating it? Commented Dec 8, 2010 at 16:46
  • @Andrey - OP updated the question to use innerHTML (almost) correctly. @user - You have innerHtml instead of innerHTML. Please post actual code. Commented Dec 8, 2010 at 16:48
  • You should link us to the real page ( or, obviously if you can't link to it then replicate it ), which would save us time asking you questions since it seems something is off and there may be multiple libraries involved. Commented Dec 8, 2010 at 16:53
  • 1
    Okay, it was not returning a jQuery object like I thought it was. To be fair, it was an external templating library that had previously been correctly returning jQuery objects but an update someone made has caused it to no longer return a valid jQuery object. Commented Dec 8, 2010 at 16:57

4 Answers 4

2

Either something is modifying the jQuery object prototype, or you have a different library loaded.

Take your object, and test for the jQuery version like this:

alert( myjQueryObject.jquery ); // should give the jQuery version number

EDIT:

Additionally, you state that there's a removeAt method. jQuery doesn't have one of those, unless you mean removeAttr().

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

3 Comments

It is hard to pick a best answer when there are multiple best answers. Since I'm using this in my unit tests now, I marked it as best answer. I gave everyone else that told me it was not a jQuery object the points.
Everyone but me :) Perhaps I didn't come out and say that exactly, but I think I implied that you may have been setting the object incorrectly and yielding a JS object and not a jQ object. Glad you worked it out tho.
@Dutchie - I'll give you +1 for the effort. :o)
2

Are you sure it's a jquery object? Wrap it in $() again and .html() should exist.

it's [0].innerHTML or .get(0).innerHTML, in that innerHTML is a property not a method.

You should make sure that jquery exists by doing alert( jQuery == $) and you can check for the .jquery property.

5 Comments

OP is saying that that's the part that works, but .html() doesn't.
Yes, but technically .innerHTML() wouldn't work, which he just changed. Accurate info is necessary imo.
I meant innerHTML not innerHTML() it was a typo on my part.
Correct, innerHtml is DOM element's function, not jQuery, which makes me think OP is getting those two confused
@Andrey - I don't think so. OP is successfully pulling the DOM element out using [0] before employing innerHTML. Shows that he knows the difference.
1

That's odd; try $(myjQueryObject).html();. If that works, the object isn't really a jQuery node.

If you still can't figure out why the object lost the html() method, post the code which creates it. Then, we might be able to help.

Comments

1

How are you setting myjQueryObject?

<div id='myElement'></div>

//Good Javascript, Incorrect jQuery
myjQueryObject = document.getElementById('myElement');
myjQueryObject.innerHTML = '<b>My HTML Here</b>';

//Correct jQuery
myjQueryObject = $('#myElement');
myjQueryObject.html('<b>My HTML Here</b>');

//Compact Version of Correct jQuery
$('#myElement').html('<b>My HTML Here</b>');

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.