Results 1 to 4 of 4

Thread: Need help with syntax

  1. #1
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with syntax

    Howdy all...

    Here's a [very] stripped down version of a function I'm trying to create... just enough to get the gist of my problem.

    Simply put, if you pass the letter "a" into the textbox and click Execute, I want the alert to pop "Alpha". If b is passed into the function, I want the alert to pop "Beta", etc... I know I'm missing something in the red area (below)... but can't for the life of me figure out what...

    Code:
    <input type="text" id="letter" size="10">
    <input type="button" id="button1" value="execute" onclick="displayit(document.getElementById('letter').value)">
    Code:
    <SCRIPT language=javascript>
    
    function displayit(letter)
    {
    	var targeta,targetb,targetc,showthis
    
    	targeta="Alpha"
    	targetb="Beta"
    	targetc="Gamma"
    	showthis='target'+letter
    	{
    		alert(showthis);
    	}
    }
    
    </SCRIPT>
    Any help would be greatly appreciated.

    Thanks,

    - I

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    There are better ways to do this, at least in theory but the quickest code fix is:

    Code:
    function displayit(letter)
    {
    	var targeta,targetb,targetc,showthis
    
    	targeta="Alpha"
    	targetb="Beta"
    	targetc="Gamma"
    	showthis=eval('target'+letter)
    	{
    		alert(showthis);
    	}
    }
    The problem being that in order to represent the variable name's contents, rather than the literal value of the string, something must be done to let the script know this is what you are after. It would be better to do something like:

    Code:
    if (letter=='a')
    showthis='Alpha'
    for each letter, or to use the case convention/method.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply, John - that's actually what I ended up settling on. Here is my final product:

    Code:
    function quartile(letter)
    //Color-codes all displayed results based on their raw value vs their target values. Called in the setfunction() function. (aka Quartile colors)
    {
    	var raw = Array(6);
    		raw[0] = "Metric"+letter+letter+"1";
    		raw[1] = "Metric"+letter+letter+"2";
    		raw[2] = "Metric"+letter+letter+"3";
    		raw[3] = "Metric"+letter+letter+"4";
    		raw[4] = "Metric"+letter+letter+"5";
    		raw[5] = "Metric"+letter+letter+"6";
    		
    	var display = Array(6);
    		display[0] = "Metric"+letter+"1";
    		display[1] = "Metric"+letter+"2";
    		display[2] = "Metric"+letter+"3";
    		display[3] = "Metric"+letter+"4";
    		display[4] = "Metric"+letter+"5";
    		display[5] = "Metric"+letter+"6";
    
    	var target
    	{
    		if (letter == "A")
    		{target="SalesTopBox"}
    		if (letter == "B")
    		{target="CCTopBox"}
    		if (letter == "C")
    		{target="FCR"}
    		if (letter == "D")
    		{target="FCCR"}
    		if (letter == "E")
    		{target="QFCR"}
    		if (letter == "F")
    		{target="Adherence"}
    		if (letter == "G")
    		{target="AHT"}
    		if (letter == "H")
    		{target="Availability"}
    		if (letter == "I")
    		{target="PPC"}
    	}
    		
    	for (count=0; count <=5; count++)
    	{
    		if (document.getElementById(raw[count]).value <= document.getElementById(target+'Q1').value)
    		{
    			document.getElementById(display[count]).style.backgroundColor = "red";
    			document.getElementById(display[count]).style.color = "white";
    		}
    		if (document.getElementById(raw[count]).value > document.getElementById(target+'Q1').value)
    		{
    			document.getElementById(display[count]).style.backgroundColor = "yellow";
    			document.getElementById(display[count]).style.color = "black";
    		}
    		if (document.getElementById(raw[count]).value > document.getElementById(target+'Q2').value)
    		{
    			document.getElementById(display[count]).style.backgroundColor = "#6f3";
    			document.getElementById(display[count]).style.color = "black";
    		}
    		if (document.getElementById(raw[count]).value > document.getElementById(target+'Q3').value)
    		{
    			document.getElementById(display[count]).style.backgroundColor = "blue";
    			document.getElementById(display[count]).style.color = "white";
    		}
    		if (document.getElementById(raw[count]).value > document.getElementById(target+'Q4').value)
    		{
    			document.getElementById(display[count]).style.backgroundColor = "#96f";
    			document.getElementById(display[count]).style.color = "white";
    		}
    		if (document.getElementById(raw[count]).value > document.getElementById(target+'Q5').value)
    		{
    			document.getElementById(display[count]).style.backgroundColor = "gray";
    			document.getElementById(display[count]).style.color = "gray";
    		}
    	}
    }

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Just a few words of caution:

    Target and display are used by browsers to represent an attribute and a style property respectively. Though it may not cause problems in limited testing with one browser, these variable names are likely to cause a problem in some browsers in some situations. So, use different variable names, just to be on the safe side. Like change all instances of target as a variable to targ and display as a variable to disp.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •