1

I can't get JQuery's $() element constructor to work right. The following works right (using expect.js for the expect() calls):

var p = $('<div id="1"><div id="2">middle div</div></div>');
expect(p.children('div')).not.to.be(undefined); //passes
expect(p.children('div').attr('id')).to.equal('2'); //passes

But the following doesn't work

var p = $('<p id="1"><div id="2">middle div</div></p>');
expect(p.children('div')).not.to.be(undefined); //passes
expect(p.children('div').attr('id')).to.equal('2'); //fails

Any tips on how to get this to work right?

3 Answers 3

3

The HTML markup is wrong. You cannot put a block-level element like <div> into a <p> element. That's why the HTML is actually (I think this depends on the browser) most likely parsed as:

<p id="1"></p><div id="2">middle div</div>

or (in chrome):

<p id="1"></p><div id="2">middle div</div><p></p>

You can verify this easily via console.log(p);

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

1 Comment

Fixed it right up! Thanks. Here's a quick ref for what can contain what for future reference: risd.generic.cx/containment.html
1

The <div> is being ejected because it's not valid to have a <div> inside a <p>.

Comments

1

You should try this:

expect($('#1').find('div').attr('id').to.equal('2');

Edit: Niko and The System are right.. a div cannot be in a p though.

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.