2

I have a QUnit test suite and am calling

 $('#idForElement').html();

inside a QUnit test (which is registered on $(document).ready(), and then run), but this function returns null in Chrome, Firefox and IE.

However, in the script tab of Firebug in Firefox, I am able to get the expected string by adding the code as a watch expression and can see a HTML element displayed on the page and in the HTML tab of Firebug.

I've read http://api.jquery.com/html/, I have assigned this into new var, an initalised string, put the result of $('#idForElement'); into a variable and called .html() on that, tried alerts in those browsers.

What's the next step to get this working?

2
  • 1
    Did you verify that var foo = $("#idForElement"); was indeed pulling the DOM node you were looking for? Keep in mind that .html() pulls in the innerHtml of an element, so if the node contains nothing, you'll get nothing. Commented Feb 22, 2011 at 16:18
  • I did, yes as I was .appending to foo successfully. Commented Feb 22, 2011 at 16:20

2 Answers 2

4

It may depend on when you are calling this function. If you do this before the document.ready event, the element may not exist yet. Try wrapping your code like this:

$(document).ready(function () {
    //your code goes here
});
Sign up to request clarification or add additional context in comments.

1 Comment

It's inside a QUnit test which are registered inside a ready block, then called, so it's not that.
3

Rookie error.

Was calling $('#idForElement'), but setting the var to $('idForElement').

It turns out you can append to an empty wrapped set.

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.