I am trying to append the values to dropdown using below line of javascript code the values that i get in the option are correct but the issue is with the code in my aspx page
ddlCountries.appendChild(option);
Below is my aspx code
I am trying to append the values to dropdown using below line of javascript code the values that i get in the option are correct but the issue is with the code in my aspx page
ddlCountries.appendChild(option);
Below is my aspx code
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted May 27, 2023, 12:42 AM EDT
Try below code:
// Get a reference to the dropdown element var dropdown = document.getElementById(''); // Create a new option element var option = document.createElement('option'); option.value = 'value1'; // Set the value of the option option.text = 'Option 1'; // Set the text of the option // Append the option to the dropdown dropdown.appendChild(option);Thanks
avair44 reputationPosted May 27, 2023, 2:25 PM EDT
Tried this as well but it's not working its ext gridview
Amit MohantyRank #112 · 20K reputationPosted May 29, 2023, 1:52 AM EDT
try this once:
var ddlCountries = document.getElementById(''); var option = document.createElement("option"); option.text = "Option Text"; option.value = "Option Value"; ddlCountries.appendChild(option);avair44 reputationPosted May 29, 2023, 4:08 AM EDT
getting error as Uncaught TypeError: Cannot read properties of null (reading 'appendChild')
Amit MohantyRank #112 · 20K reputationPosted May 29, 2023, 4:13 AM EDT
The error message "Uncaught TypeError: Cannot read properties of null (reading 'appendChild')" indicates that the
ddlCountrieselement is null. Make sure the JavaScript code is executed after theddlCountriesdropdown is rendered on the page. Placing the JavaScript code at the end of the page can help ensure that the dropdown element exists before accessing it.avair44 reputationPosted May 29, 2023, 4:36 AM EDT
this append child is at the end of the function only