Attila Naghi
Attila Naghi

Reputation: 2686

How to find the closest input with JQuery?

hi this is my html code :

<div class="transe_row">
    <div class="litransa1" style="cursor:pointer;">Servicii programare Transa 1</div>
    <div class="litransa suma" style="cursor:pointer;"> Adauga Suma</div>
    <input type ="text"   placeholder ="adauga suma transa" style ="display:none;">
</div>

and this is my js :

<script type="text/javascript">
    $(document).ready(function (){
            $(".litransa.suma").click(function() {
                 $('.litransa.suma').find(closest('input')).css("display","inline");
        });
    });
</script>

I just want to change the the css from the input by clicking the .listransa.suma class. Why my code doesn't work ?thx

Upvotes: 0

Views: 789

Answers (2)

.next() and this keyword

$(document).ready(function () {
    $(".litransa.suma").click(function () {
        $(this).next('input').css("display", "inline");//or $(this).next('input').show();
    });
});

this refer to the current element clicked

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

You need to use .siblings() for finding sibling elements:

 $(document).ready(function () {
  $(".litransa.suma").click(function () {
     $(this).siblings('input').css("display","inline")
  });
});

Upvotes: 1

Related Questions