Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi, from the bottom of this page I'm trying to change last 'select' name from "Indice e sommario" to "Classificazione".

Thank you!

What I have tried:

JavaScript
const select = document.querySelector('select[name="canale"]')[3];
 select.value = 'CL';
Posted
Updated 26-Mar-23 22:45pm

1 solution

The method querySelector() returns only a single HTML element as a result, or null if no possible match was found. You're using it as if it returns multiple values, which it won't.

If there are multiple elements with the same name, instead use querySelectorAll() which will return a collection of elements. Otherwise, I'd maybe look at giving these selects unique id attributes and selecting them that way. Relying on the element being at a fixed index in the query selector could probably spell trouble in the future.

// Use the all method
const select = document.querySelectorAll('select[name="canale"]')[3];
select.value = 'CL';

// Better yet, give the element a unique ID
const select = document.getElementById('canale3');
select.value = 'CL';
 
Share this answer
 
Comments
Parvares 28-Mar-23 4:07am    
Perfect, thanks, Chris. I was looking for the ID but didn't find it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900