Solved

javascript closest match wildcard

Posted on 2011-02-25
6
394 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now