Solved

javascript closest match wildcard

Posted on 2011-02-25
6
399 Views
Last Modified: 2012-05-11
i have a script that looks up an image name based sting name

but i would like it to be able to make a close match using a wildcard

document.getElementById('resultImg').src='flags/'+callCost[0]+'.png';

how can i set callcost[0] to be a closest match to and image in a folder containing *.png images

callcost list

Afghanistan-Kabul
Afghanistan-Mobile
Afghanistan-Mobile-Areeba
Afghanistan-Mobile-Etisalat
Afghanistan-Mobile-Roshan

folder images
Afghanistan.png

so the callcost selection matches the folder image name image
0
Comment
Question by:daz1234
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34985326
I am trying to understand.

You want to find the first part of the string, everything up to the first "-" dash character:

Afghanistan-Kabul
Afghanistan-Mobile
Afghanistan-Mobile-Areeba
Afghanistan-Mobile-Etisalat
Afghanistan-Mobile-Roshan

and then you want to use that Afghanistan string to load an image called Afghanistan.png?

Is it like this:

France-Paris would go to France.png
England-London would go to England.png
England-London-Heathrow would also go to England.png
England (all by itself) would also go to England.png


Do I understand the question?
0
 

Author Comment

by:daz1234
ID: 34986178
Yes that it
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34986817
Perhaps this will help.

Here is a function called GetImageName that will return the "base" image name given a string that may have dashes in it.

All five examples below return "Afghanistan.png".

<script>
function GetImageName(s)
{
	var dashPos = s.indexOf('-');
	if ( dashPos == (-1) ) return s+'.png';
	return s.substring(0, dashPos)+'.png';
}


alert(GetImageName('Afghanistan-Kabul'));
alert(GetImageName('Afghanistan-Mobile'));
alert(GetImageName('Afghanistan-Mobile-Areeba'));
alert(GetImageName('Afghanistan-Mobile-Etisalat'));
alert(GetImageName('Afghanistan'));

</script>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:daz1234
ID: 34987382
this is the existing script i have how would i integrate your function into it

<script language="JavaScript" type="text/javascript">
                              <!--
                              function callRef() {
                                    // COUNTRY SELECTOR
                                    if (document.selector1.select1.options[document.selector1.select1.selectedIndex].value != "0") {
                                          var callCosts = document.selector1.select1.options[document.selector1.select1.selectedIndex].value;
                                          callCost = callCosts.split('?');
                                          //= this is were i want enter your function=> document.getElementById('resultImg').src='flags/90x90/'+callCost[0]+'.png';
                                          document.getElementById('rate1').innerHTML=''+callCost[1]+' &#36;/min';
                                          document.getElementById('rate2').innerHTML=''+callCost[2]+' &#36;/min';      
                                    }
                              }
                              //-->
                        </script>
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34987871
I think this is what ou're looking for
<script language="JavaScript" type="text/javascript">
<!--
function GetImageName(s) {
    var dashPos = s.indexOf('-');
    if ( dashPos == (-1) ) return s;
    return s.substring(0, dashPos);
}

function callRef() {
    // COUNTRY SELECTOR
    if (document.selector1.select1.options[document.selector1.select1.selectedIndex].value != "0") {
        var callCosts = document.selector1.select1.options[document.selector1.select1.selectedIndex].value;
        callCost = callCosts.split('?');

        //= this is were i want enter your function=> document.getElementById('resultImg').src='flags/90x90/'+callCost[0]+'.png'; 

        document.getElementById('resultImg').src= 'flags/90x90/' + GetImageName(callCost[0]) + '.png'; 

        document.getElementById('rate1').innerHTML=''+callCost[1]+' &#36;/min';
        document.getElementById('rate2').innerHTML=''+callCost[2]+' &#36;/min';      
    }
}
//-->
</script> 

Open in new window

0
 

Author Closing Comment

by:daz1234
ID: 34999065
worked like a treat thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question