Solved

javascript closest match wildcard

Posted on 2011-02-25
6
404 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

707 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