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

396
Views
How to change a Div content when selecting a value from a select with a jQuery?

Hi I'm new to jQuery and I'm trying to solve this:

When selecting a value from the dropdown (<select>), you need to change the content of the div tag (in red) with the selected value. Please use jQuery to solve this test, and please don't add any id/class to the above HTML code!

I already wrote a script but it does not work, can you help please

var content = $( "div:gt(5)" );
$( "select" ).on( "click", function( event ) {
  content.html('6 products');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toolbar-wrapper">
              <div>
                <div>
                  <label>Product Count</label>
                  <div class="select-group">
                    <select>
                      <option value="4 products" selected="selected">4 products</option>
                      <option value="6 products">6 products</option>
                      <option value="8 products">8 products</option>
                    </select>
                  </div>
                </div>

                <div>4 products</div>
              </div>
            </div>

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use this.value in the event handler to get the value that was selected in the dropdown.

When you select something from the dropdown, the change event is triggered, not click.

div:gt(5) is not the correct selector for the div where the result should be displayed. Numbering starts from 0, so it's DIV number 4. You can use .eq(4) to select that.

var content = $("div").eq(4);
$("select").on("change", function(event) {
  content.html(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toolbar-wrapper">
  <div>
    <div>
      <label>Product Count</label>
      <div class="select-group">
        <select>
          <option value="4 products" selected="selected">4 products</option>
          <option value="6 products">6 products</option>
          <option value="8 products">8 products</option>
        </select>
      </div>
    </div>

    <div>4 products</div>
  </div>
</div>

about 4 years ago · Santiago Trujillo Report

0

You can target with > & position selectors.

$( "select" ).on( "change", function( event ) {
  $('.toolbar-wrapper > div > div:nth-child(2)').html(event.target.value);
});

Fiddle!

about 4 years ago · Santiago Trujillo 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!