Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Changes unexpectedly

Status
Not open for further replies.

prgmrgirl

Programmer
Feb 12, 2004
119
US
Hey guys!

This is a weird one. I'm not sure what to make of it.

I have a two functions like so:
Code:
<input type='radio' name='foo' value='1234-1' onclick='myFunction(this.value)'/>
<input type='radio' name='foo' value='1234-2' onclick='myFunction(this.value)'/>

function myFunction(val)
{
	//do some stuff that doesn't change the value of val
	otherFunction(val)
}

function otherFunction(val)
{
	alert(val)

}

What I am expecting in the alert: 1234-1 or 1234-2 depending on what was clicked.

What I am getting is: 1234.

?? I'm soo confused. What's happening to info?? It doesn't make any sense!

Going crazy and appreciate any help on this one.

Thanks!
prgmrgirl
 
Any chance that you are using the parseInt function, or your function is somehow expecting a number? parseInt will return the number represented up until the first non-numeral character (in your case, a dash). Consider this example:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var a = "1234-1"
a = parseInt(a, 10);
alert(a);

</script>
<style type="text/css"></style>
</head>
<body>
</body>
</html>
The result is "1234".

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Without seeing your REAL code, it's difficult to tell what's going on. You should copy and paste your code in here so we can see what's happening line by line.

Lee
 
Thanks for your responses.

Kaht, no I am not using parseInt. I have been trying to send through that variable in different ways, each time, I either get a weird error or the result above.

trollacious, here's the actual code (please note that some of the functions are not in there, just the three involved).

Code:
function getLocations(custID)
{
	//SQL string that will retrieve the locations for a particular customer 
	var strSQL = "SELECT * FROM qryCustomerLocation WHERE custID = '" + custID + "'"
	
	//get the locations
	var locations = getRecordset(strSQL)

	//select the element placeholder
	var loc = document.getElementById("locations")
	
	var size = locations.ubound(2)
	//counter
	var row = 0
	
	//HTML to place at that locations
	var strHTML = "<p>Choose a location for this customer: <br>"
	var strEvent = "var val=this.value; var fname=this.name; getChosenOption(val, fname);"
	
	while(row<=size)
	{
		var custLoc = locations.getItem(0,row) + '-' + locations.getItem(1,row)
		strHTML=strHTML + "<input type='radio' value='" + custLoc + "' name='getLocProd' onclick='" + strEvent + "'/>" + locations.getItem(2,row) + "<br>"
		row++
	}
	
	//closing tags
	strHTML = strHTML + "</p>"
	
	loc.innerHTML = strHTML
}

function getChosenOption(value, fname)
	{
		//creates a string to be used to call the appropriate function
		var strFunctionCall = fname + "(" + value + ")"
		//evaluate the string as an actual function call
		eval(strFunctionCall)
	}


function getLocProd(value)
{
	alert(value)
}

I have tried to send the value through directly like this:

onclick='getLocProd(this.value)'

but I get a generic kind of error (specifying the first line, first character which has nothing to do with anything that I am calling) so I don't know. The way I did it above works except that I only get the beginning of the string. What's strange is that I put an alert in the getChosenOptions function to see what the value is when it gets passed in and it gives the correct value. Somehow when it's passed into getLocProd, it cuts off the last of the string.

Thanks everyone!

 
It's that dirty, nasty eval that is giving you problems. eval is almost always unnecessary.

Eval is treating your string as it's literal interpretation, which is 1234-1, which equals 1233 - which I assume is what you're seeing alerted. You can fix this by maintaining your value's "string status".

Code:
fname + "([!]'[/!]" + value + "[!]'[/!])"

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
ROFLMAO

Thanks Kaht! I totally missed that extra set of 's.

Off to try it and see if it works!
 
Thanks for the advice, kaht. I got rid of that "dirty nasty eval."

=)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top