-1

Trying to do a simple str_replace() in javascript and i can't seem to figure out why it is not working. I am making a form where on sign up the user can select a country and it auto loads up the correct flag for that country. Everything works fine until you select a country that has spaces in the name. The flag images use a - in the place of a space. help...

Javascript Code:

$flagimg = '<img id="current_flag" name="current_flag" src="images/icons/flags/United-States.png" width="20px" />';

<script type="text/javascript">

function LoadFlag(FlagName) {
    var oFlag = window.document.getElementById("country");
    var sFlagImg = oFlag.options[oFlag.selectedIndex].value;

    window.document.getElementById(FlagName).src = "images/icons/flags/" + sFlagImg + ".png";
}

</script>

HTML Code:

<select id="country" name="country" onchange="javascript: LoadFlag('current_flag');"><?php echo getCountries(); ?></select>&nbsp; <?php echo $flagimg; ?>

Where do I put the str_replace()?

i tried:

window.document.getElementById(FlagName).src = "images/icons/flags/" + str_replace(' ', '-', sFlagImg) + ".png";

and it does not work.

Image Names:

United-States.png
United-Kingdom.png

Select Box Option Values:

United States
United Kingdom
rackemup420
  • 1,600
  • 2
  • 15
  • 37

1 Answers1

0

Found the answer here "Replace all spaces in a string with '+'" sorry for wasting time.

window.document.getElementById(FlagName).src = "images/icons/flags/" + sFlagImg.split(' ').join('-') + ".png";

solved it!

Community
  • 1
  • 1
rackemup420
  • 1,600
  • 2
  • 15
  • 37