-2

im trying to get the parent of this input but it just wont, i get the label at its most. what im i doing wrong?

<div class="input req">

            <p>
                <label>                   
                    <%= Html.TextBoxFor(model => model.Street, new { TABINDEX = 1,data_validate = "required|min[2]" })%>
                </label>
            </p>

            <p class="msg">
            </p>

        </div>

JQUERY

alert($('#Street').parent('div.input').length);

EDIT: i should add this is all work in a thickbox and i get length 1 if i take parent('label')

EDIT2: missed this link that is posted How to find a parent with a known class in JQuery?

-2 minus for what?

Community
  • 1
  • 1
Dejan.S
  • 18,571
  • 22
  • 69
  • 112

4 Answers4

1
alert($('#Street').parents('div.input:first').length);

which is similar to :

alert($('#Street').closest('div.input').length);

parent just look for the immediate parent. You should use one of the above methods instead

yokoloko
  • 2,820
  • 3
  • 20
  • 27
0
alert($('#Street').parents('div.input').length);
                         ^

parent() will go up one level, on the other hand .parents() will go up until it matches the selector.

In your case, the immediate parent is the element p, not the div.input.

Shef
  • 44,808
  • 15
  • 79
  • 90
0

try

alert($('#Street').closest('div.input').length);
Rafay
  • 30,950
  • 5
  • 68
  • 101
0

try

alert($('#Street').parent('label').parent('p').parent('div.input').length);

Parent only looks at the element directy above it, this way you take the steps necessary to climb the DOM tree.

Jesse van Assen
  • 2,240
  • 2
  • 16
  • 19