0

Consider the following HTML

<fieldset>
    <legend>FOO</legend>
    <div> <!-- this is not static code...it's generated by jQuery-->
        <input type="checkbox" id="chkBox" />
    </div>
</fieldset>

When I run this jQuery

console.log($("#chkBox").parent());

I get [Fieldset] instead of the expected [div]...and I have no idea why!?

EDIT: Just a quick point here...the <div> tag is being generated by jQuery...so in reality, the fieldset IS the parent (hard-coded)...so how would I get the dynamically created parent?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

2 Answers2

1

You mention the <div> tag is generated dynamically. If the checkbox is not generated dynamically as well, it will cause the added <div> tag to not properly wrap the checkbox.

View the content in Chrome's Inspect Element tool, or any other browser's equivalent (i.e. FireBug) and make sure the dynamically generated content alters the DOM to create the output you expect as you posted in your question above.

Hope this helps.

War10ck
  • 12,387
  • 7
  • 41
  • 54
  • Excellent. My logic was a bit backward. it was performing the parent() operation before nesting the input tag into the div, hence, it was getting the fieldset as the parent. +1 and accepted. Thanks! – Matthew Layton Apr 03 '13 at 16:08
  • No problem buddy. Glad it worked for you. :) – War10ck Apr 03 '13 at 16:08
0

There's a typo in your HTML

<legend>FOO</ledgend>

This could be causing parsing errors which means your DOM isn't what you think it is.

tinyd
  • 952
  • 6
  • 9