Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:41+00:00 2026-05-27T15:14:41+00:00

I was wondering how to synchronize the values and text of two elements. For

  • 0

I was wondering how to synchronize the values and text of two elements. For instance,

<select id="box1" onchange="sync();">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select id="box2">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

and then sync(); would look something like….

function sync()
{
box2.selected = box1.selected;
}

Any idea how I would do this?
Thanks,
Matthew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:14:41+00:00Added an answer on May 27, 2026 at 3:14 pm

    One possible approach:

    function sync(el1, el2) {
        // if there is no el1 argument we quit here:
        if (!el1) {
            return false;
        }
        else {
            // caching the value of el1:
            var val = el1.value;
    
            // caching a reference to the element with
            // with which we should be synchronising values:
            var syncWith = document.getElementById(el2);
    
            // caching the <option> elements of that <select>:
            var options = syncWith.getElementsByTagName('option');
    
            // iterating over those <option> elements:
            for (var i = 0, len = options.length; i < len; i++) {
    
                // if the value of the current <option> is equal
                // to the value of the changed <select> element's
                // selected value:
                if (options[i].value == val) {
    
                    // we set the current <option> as
                    // as selected:
                    options[i].selected = true;
                }
            }
        }
    }
    
    // caching the <select> element whose change event should
    // be reacted-to:
    var selectToSync = document.getElementById('box1');
    
    // binding the onchange event using an anonymous function:
    selectToSync.onchange = function(){
    
        // calling the function:
        sync(this,'box2');
    };
    
    function sync(el1, el2) {
      if (!el1) {
        return false;
      } else {
        var val = el1.value;
        var syncWith = document.getElementById(el2);
        var options = syncWith.getElementsByTagName('option');
        for (var i = 0, len = options.length; i < len; i++) {
          if (options[i].value == val) {
            options[i].selected = true;
          }
        }
      }
    }
    
    var selectToSync = document.getElementById('box1');
    selectToSync.onchange = function() {
      sync(this, 'box2');
    };
    <select id="box1">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <select id="box2">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>

    JS Fiddle demo.

    Or, revised and updated somewhat:

    function sync() {
    
      // caching the changed element:
      let el = this;
    
      // retrieving the id of the element we should synchronise with
      // from the changed-element's data-syncwith custom attribute,
      // using document.getElementById() to retrieve that that element.
      document.getElementById( el.dataset.syncwith )
        // retrieving the <options of that element
        // and finding the <option> at the same index
        // as changed-element's selectedIndex (the index
        // of the selected <option> amongst the options
        // collection) and setting that <option> element's
        // selected property to true:
        .options[ el.selectedIndex ].selected = true;
    }
    
    // retrieving the element whose changes should be
    // synchronised with another element:
    var selectToSync = document.getElementById('box1');
    
    // binding the snyc() function as the change event-handler:
    selectToSync.addEventListener('change', sync);
    
    function sync() {
      let el = this;
      document.getElementById(el.dataset.syncwith).options[el.selectedIndex].selected = true;
    }
    
    var selectToSync = document.getElementById('box1');
    selectToSync.addEventListener('change', sync);
    <select id="box1" data-syncwith="box2">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <select id="box2">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>

    JS Fiddle demo.

    Note that this approach does assume – and requires – that the <option> elements are in the same order.

    To update the original approach, where the order is irrelevant, using ES6 approaches (and the same data-syncwith custom attribute approach):

    function sync() {
      // caching the changed element (since
      // we're using it twice):
      let el = this;
    
      // retrieving the id of the element to synchronise 'to' from 
      // the 'data-syncwith' custom attribute of the changed element,
      // and retrieving its <option> elements. Converting that
      // Array-like collection into an Array using Array.from():
      Array.from(document.getElementById(el.dataset.syncwith).options)
        // Iterating over the array of options using
        // Array.prototype.forEach(), and using an Arrow function to
        // pass the current <otpion> (as 'opt') setting that current
        // <option> element's selected property according to Boolean
        // returned by assessing whether the current option's value
        // is (exactly) equal to the value of the changed element:
        .forEach(opt => opt.selected = opt.value === el.value);
    }
    
    var selectToSync = document.getElementById('box1');
    selectToSync.addEventListener('change', sync);
    
    function sync() {
      let el = this;
      Array.from(document.getElementById(el.dataset.syncwith).options).forEach(opt => opt.selected = opt.value === el.value);
    }
    
    let selectToSync = document.getElementById('box1');
    selectToSync.addEventListener('change', sync);
    <select id="box1" data-syncwith="box2">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <select id="box2">
      <option value="1">One</option>
      <option value="3">Three</option>
      <option value="2">Two</option>
    </select>

    JS Fiddle demo.

    If you look at the HTML in the Snippet you’ll see that I switched the positions of <option> elements in the second <select> element to demonstrate that the <option> position doesn’t matter in this latter approach.

    References:

    • Array.from().
    • Array.prototype.forEach().
    • Arrow functions.
    • document.getElementById().
    • EventTarget.addEventListener().
    • for loop.
    • HTMLElement.dataset.
    • HTMLSelectElement.
    • let statement.
    • var.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if synchronize (lock) { ... } Where lock is an instance
I'm wondering if there's a way in Java to synchronize using two lock objects.
Wondering if there is any Text to Speech software available as a plug in
Wondering if there's any not-too-hard way to edit non-form text in html 4. I
Im wondering how it would be possible to synchronize desktop and mobile application. For
I am using the CountDownLatch to synchronize an initialization process between two threads and
I'm looking for a library that would allow me to synchronize text in real-time
I'm wondering about writing an application in Java which can synchronize files across my
I've been wondering if we there is an native or generic way to synchronize
Wondering if anyone has gotten the infamous database is locked error from Trac and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.