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
  • 740
  • 8
  • 23

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
  • 319,460
  • 37
  • 290
  • 318
2

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

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

stackoverfloweth
  • 6,177
  • 4
  • 36
  • 61
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,058
  • 3
  • 28
  • 49