Programming Dilemma
In my HTML, there is a form structured like this:
<label for="alternativeGraph">Alternative graphs could be seen here:</label>
<select id="selGraph" onchange="graphUpdate()" aria-label="Graph">
<option value="1" selected="selected">Graph 1 (default)</option>
<option value="2">Graph 2</option>
<option value="3">Graph 3</option>
<option value="4">Graph 4</option>
<option value="5">Graph 5</option>
</select>
<button type="button" onclick="setDefault()"> Change default graph</button>
The aim is to have Graph 1 load as the default on page load and allow users to change the default graph using the setDefault() function. Here's the JavaScript involved:
function render(filename) {
fetch(filename).then(response => response.text()).then(textAsString =>
renderString(textAsString));
}
function graphUpdate(){
let value = document.querySelector('#selGraph');
let graph = ["graph_1.gv", "graph_2.gv", "graph_3.gv", "graph_4.gv", "graph_5.gv"]
render(graph[value.selectedIndex]);
}
// function setDefault(){ # I am not sure about what should be added here...
// let new_default_graph = document.querySelector("#selGraph");
// new_default_graph.value =
// }
Challenges Faced
The primary issue encountered is that upon website loading, despite choosing Graph 1 ("graph_1.gv" file) as the default, it does not display until clicking the dropdown menu. Other graphs are loaded correctly.
Inquiries:
Is there a method to pre-load the graph based on the initial selection? Additionally, how can the setDefault()
function be modified so that when users select an option like 3, the website remembers this choice as the default upon refreshing?