Make sure to enclose the text in the link tag in quotes to avoid any confusion when searching for the scotislands
variable.
onClick="flag('scotislands');"
After that, adjust the innerHTML assignment as follows:
document.getElementById("flag").innerHTML="<img src='images/flags/" + nation + ".jpg'>"
Note the use of single quotes within the URL and double quotes around the text.
For a more elegant solution without embedding code in the markup, consider using a framework like jQuery:
<a href="#scotislands" class="flag-identifier">Scotislands</a>
Then, in JavaScript, create a handler for all similar links:
$(function() {
$('a.flag-identifier').click( function() {
var flag = $(this).attr('href').replace('#','');
$('#flag').html( '<img src="images/flags/' + flag + '.jpg" />' );
return false;
});
});
This script sets up a click event that extracts the flag name from the anchor's href attribute, updates the content of the designated flag
element with the composed image URL, and can also handle setting the nation
variable if needed within the click function.