Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Show input value in the closest div with jquery

I want to return the value of each input its closest div. Each div has the different name but same div class next to it. The problem is I cannot find the closest div class with jquery. But the value from typing keyup sent succesfully.

        $('.bk_name').on('keyup', function() {
          var q = $(this).val();
          $(this).parent().closest('div.resp').html(q);
      console.log(q);
        }); //keyup
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-sm-5">
  <label>Name1</label>
  <input type="text" class="bk_name" name="bk_name[1]" />
  <div class="resp"></div>
</div>


<br />
<div class="col-sm-5">
  <label>Name2</label>
  <input type="text" class="bk_name" name="bk_name[2]" />
  <div class="resp"></div>
</div>

For the fiddle here : https://jsfiddle.net/s85n42eo/1/

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use $(this).next('div.resp').html(q) or $(this).parent().find('div.resp').html(q)

The problem is that you are using .closest() it will search for parents, not look for children.

Demo

$('.bk_name').on('keyup', function() {
  var q = $(this).val();
  console.log($(this).parent().find('')
  $(this).next('div.resp').html(q);
  console.log(q);
}); //keyup
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-sm-5">
  <label>Name1</label>
  <input type="text" class="bk_name" name="bk_name[1]" />
  <div class="resp"></div>
</div>


<br />
<div class="col-sm-5">
  <label>Name2</label>
  <input type="text" class="bk_name" name="bk_name[2]" />
  <div class="resp"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You should use

 .find()

when yo want to search for a nested element.

since you bubble up 1 element using .parent() you get to the <div class="col-sm-5"> element. from there you are trying to reach a child element <div class="resp"> - you can do that with .find

.closest traverses up through its ancestors in the DOM tree while .find does the opposite

$('.bk_name').on('keyup', function() {
          var q = $(this).val();
          $(this).parent().find('div.resp').html(q);
      console.log(q);
        }); //keyup
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-sm-5">
  <label>Name1</label>
  <input type="text" class="bk_name" name="bk_name[1]" />
  <div class="resp"></div>
</div>


<br />
<div class="col-sm-5">
  <label>Name2</label>
  <input type="text" class="bk_name" name="bk_name[2]" />
  <div class="resp"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!