Home > Enterprise >  Replace country code with country with jQuery
Replace country code with country with jQuery

Time:05-20

I'd like to replace country abbreviations with country names but can't get it to work - what am I doing wrong?

https://jsfiddle.net/f6jq7eht/1/

HTML

<div >
        <span >Country/Region</span>
        <div>IT</div>
</div>

jQuery

$(document).ready(function() {  
        var text = $(".country div").html();
        text = text.replace('CA', 'Canada')
        text = text.replace('DE', 'Germany')
        text = text.replace('PO', 'Poland')
        text = text.replace('SG', 'Singapore')
        text = text.replace('UK', 'United Kingdom')
        text = text.replace('US', 'United States')
        text = text.replace('DK', 'Denmark')
        text = text.replace('MX', 'Mexico')
        text = text.replace('IT', 'Italy')
});

CodePudding user response:

$(document).ready(function() {  
  var text = $(".country div").html();
  text = text.replace('CA', 'Canada')
  text = text.replace('DE', 'Germany')
  text = text.replace('PO', 'Poland')
  text = text.replace('SG', 'Singapore')
  text = text.replace('UK', 'United Kingdom')
  text = text.replace('US', 'United States')
  text = text.replace('DK', 'Denmark')
  text = text.replace('MX', 'Mexico')
  text = text.replace('IT', 'Italy');
            
  $(".country div").html(text);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div >
  <span >Country/Region</span>
  <div>IT DE CA US</div>
</div>

  • Related