Reputation: 75
I'm quite new to JavaScript, but am attempting to write a script that will convert text numbers to an image (of the number). How might I go about this?
P.s. I did find this code online, but it seems wishy-washy.
<script language="JavaScript1.1" type="text/javascript">
function phils_image_price(input)
{
//declare variables
var output=""
var position=0
var chr=""
//loop
for (var position=0; position < input.length; position++)
{
chr=input.substring(position,position+1)
//replace
if (chr == '£') {output=output + '<img border="0"
src="img/pound.gif">'}
else if(chr == '0') {output=output + '<img border="0"
src="img/0.gif">'}
else if(chr == '1') {output=output + '<img border="0"
src="img/1.gif">'}
else if(chr == '2') {output=output + '<img border="0"
src="img/2.gif">'}
else if(chr == '3') {output=output + '<img border="0"
src="img/3.gif">'}
else if(chr == '4') {output=output + '<img border="0"
src="img/4.gif">'}
else if(chr == '5') {output=output + '<img border="0"
src="img/5.gif">'}
else if(chr == '6') {output=output + '<img border="0"
src="img/6.gif">'}
else if(chr == '7') {output=output + '<img border="0"
src="img/7.gif">'}
else if(chr == '8') {output=output + '<img border="0"
src="img/8.gif">'}
else if(chr == '9') {output=output + '<img border="0"
src="img/9.gif">'}
else if(chr == '.') {output=output + '<img border="0"
src="img/dot.gif">'}
else {output=output + chr}
}
return output;
}
</script>
</head>
<body>
<script language="JavaScript1.1" type="text/javascript">
document.write('£12345678.90')
document.write('<br>')
document.write(phils_image_price('£12345678.90'))
</script>
Upvotes: 0
Views: 2330
Reputation:
function textNumbersToImages(text) {
var output = '';
// You could define a list of filenames here
var images = ['0.gif', '1.gif', '2.gif', '3.gif',
'4.gif', '5.gif', '6.gif', '7.gif', '8.gif', '9.gif'];
// We eliminate all characters that are not a number
var nums = text.replace(/\D/g, '');
// Now we iterate over all those numbers
for (var i=0; i < nums.length; i++) {
output += '<img src="' + images[nums.charAt(i)] + '" />';
}
// We return the constructed string, that is a list of image tags
return output;
};
Upvotes: 1
Reputation: 318212
function phils_image_price(input) {
var output = ""
for (var i = 0; i < input.length; i++) {
var chr = input.substring(i, i + 1)
if (chr == '£') {
output += '<img border="0" src="img/pound.gif">';
} else if (chr == '.') {
output += '<img border="0" src="img/dot.gif">';
} else {
output += '<img border="0" src="img/'+(chr+1)+'.gif">';
}
return output;
}
Upvotes: 1