Solved

javascript closest match wildcard

Posted on 2011-02-25
6
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rotate images for Web Page 11 28
Html using "Or" in condition 3 29
.CSS HTML Help 3 32
Extract Proxy Server  from Proxy PAC file in DOS BATCH File 4 25
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

696 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