0

It's a very simple question, but I dont know exactly how to get from .

<div>
    <input>
</div>
<p id="pepe"></p>

The following :

$("#pepe").prev()

gives me the div, how I get the first child (input)

Shane_Yo
  • 770
  • 8
  • 24

3 Answers3

6

As you've seen prev() gets the previous sibling element. To get the input from there you need to use find(), along with :first assuming there will be multiple within your actual working HTML:

$("#pepe").prev().find('input:first');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2

$("#pepe").prev() is getting you the <div>

try $("#pepe").prev().children('input');

stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69
1

In your code .prev() point out the div element not the input. if you have search input means use .find() in jquery

$("#pepe").prev().find('input')
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49