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.
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>
4
Answers
You are setting the value of both dropdowns to selectedValue which is present in either dropdown or dropdown2 but not both. You need to check if the options are present in the dropdown before setting its value.
A simple check will do this:
The following doesn’t do what you expect:
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:
will do.
Then there is:
This selects td elements, not rows. So use:
But now:
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.
BTW, using non–standard attributes to store data is not a good idea, it’s why data– attributes were invented.
I would change your selector to work off #table tr instead of #table td. Then you you can set the dropdown values respectively based off the value attribute of the appropriate positioned cell ():
First, let’s see how it can be made to work
Explanation:
const dropdowns = document.getElementById('dropdown') && ('dropdown_2');
was attempting to get the element whoseid
isdropdown
and, if not found, then evaluated to thedropdown
string, instead of getting the dropdowns, as you intended"#dropdown, #dropdown_2"
instead and destructuring the result into similarly named attributestableRows
to actually representtr
elements rather thantd
elements inside your tabletr
instead of sometd
td
elements, convert the array-like-object I received in the answer into an array via[...this.querySelectorAll('td')]
and then map the items to theirvalue
attribute, destructuring the result into variables namedoption
andvalue
, respectivelyNow, you had some other criteria mentioned in your question as well and that might be important to you. You said that you prefer the dropdowns and the table to be on different HTML pages. If this is indeed a desire you want to accomplish, then you will need to either message between pages, use window.open or communicate between an
iframe
and the page that contains it if your are on the same origin.