By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to set the value of two dropdowns by clicking on a table row?
JavaScript

Javascript – How to set the value of two dropdowns by clicking on a table row?

Admin
Last updated: 2024/02/10 at 2:44 PM
Admin
Share
3 Min Read
How to set the value of two dropdowns by clicking on a table row?

Problem:

Further to my previous question, I’m now trying to find out a way to dynamically change the selected value of two dropdown-menus by clicking on a table row/cell, preferably located on another html page.

Contents
Problem:Solution:

The code below shows my attempt to get it to work, but it doesn’t show the values of both dropdown-menus simultaneously, instead they get blanked by each other when clicking on any table row.

Apparently, the dropdowns get affected by the columns instead of by the rows.

// Get references to the dropdown and table
const dropdowns = document.getElementById('dropdown') && ('dropdown_2');
const tableRows = document.querySelectorAll('#table td');

// Add a click event listener to each table row
tableRows.forEach(row => {
  row.addEventListener('click', function() {

    // Get the value associated with the clicked row
    const selectedValue = this.getAttribute('value');

    // Set the dropdown's value to the selected value
    dropdown.value = selectedValue;
    dropdown_2.value = selectedValue;
  });
});
<select id="dropdown">
  <option value="option1">Option 1</option>
  <option value="option2">Option 2</option>
  <option value="option3">Option 3</option>
</select>

<select id="dropdown_2">
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
  <option value="value3">Value 3</option>
</select>

<table border="1" cellpadding="1" cellspacing="1" id="table">
  <tr>
    <th value="">Options</th>
    <th value="">Value</th>
  </tr>
  <tr>
    <td value="option1">Option1</td>
    <td value="value1">Value1</td>
  </tr>
  <tr>
    <td value="option2">Option2</td>
    <td value="value2">Value2</td>
  </tr>
  <tr>
    <td value="option3">Option3</td>
    <td value="value3">Value3</td>
  </tr>
</table>

Solution:

The following doesn’t do what you expect:

const dropdowns = document.getElementById('dropdown')&&('dropdown_2');

It sets the value of dropdowns to the string “dropdown_2”. But that doesn’t matter because dropdowns isn’t used for anything anyway. If you want a list of elements whose ID starts with “dropdown” then:

const dropdowns = document.querySelectorAll('[id^=dropdown]')

will do.

Then there is:

const tableRows = document.querySelectorAll('#table td');

This selects td elements, not rows. So use:

const tableRows = document.querySelectorAll('#table tr');

But now:

this.getAttribute('value')

won’t work because this references the row, and the value attribute is on the cells. So get the value of the cells using the row’s cells collection.

e.g.

<select id="dropdown">
  <option value="option1">Option 1</option>
  <option value="option2">Option 2</option>
  <option value="option3">Option 3</option>
</select>

<select id="dropdown_2">
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
  <option value="value3">Value 3</option>
</select>

<table border="1" cellpadding="1" cellspacing="1" id="table">
  <tr>
    <th value="">Options</th>
    <th value="">Value</th>
  </tr>
  <tr>
    <td value="option1">Option1</td>
    <td value="value1">Value1</td>
  </tr>
  <tr>
    <td value="option2">Option2</td>
    <td value="value2">Value2</td>
  </tr>
  <tr>
    <td value="option3">Option3</td>
    <td value="value3">Value3</td>
  </tr>
</table>

<script>
  // Get references to the dropdown and table
  const dropdowns = document.querySelectorAll('[id^=dropdown]')
  const tableRows = document.querySelectorAll('#table tr');

  // Add a click event listener to each table row
  tableRows.forEach(row => {
    row.addEventListener('click', function() {

      // Get the cells associated with the clicked row
      let cells = this.cells;

      // Set the dropdown values to the selected values
      dropdown.value = cells[0].getAttribute('value');
      dropdown_2.value = cells[1].getAttribute('value');
    });
  });
</script>

Expand snippet

BTW, using non–standard attributes to store data is not a good idea, it’s why data– attributes were invented.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Convert hierarchical text (with hyphens) into flare data for D3.js Javascript – Convert hierarchical text (with hyphens) into flare data for D3.js
Next Article Changing the default max filesize in Dropzone.js Javascript – Changing the default max filesize in Dropzone.js
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?