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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Stop JS from Calculating Social Sec. Number 1

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
I'm perplexed again...

I'm passing a var through a constructed event and at some point JS is converting the string to a result. How do I stop this behavior? Example Social Security Number 111-11-1111 is turning into the value -1011.

Code that constructs the event handler:
Code:
var users = eval(trim(xmlhttp.responseText));
					var str = "";
					for ( var recno in users ) {
						var selectedSSN=users[recno].LicenseeSSN;
						str += '<span onclick="putIn('+selectedSSN+')">'+selectedSSN + " is ";
						str += users[recno].LicenseeName + " ";
						str += users[recno].LicenseYear + "</span><br/>";
						}
					document.getElementById('pickList').innerHTML = str;
				}

Code that populates an input element; at this point the math operation seems to have taken place:
Code:
function putIn(selectedSSN){
		
		document.getElementById('lname').value='';
		document.getElementById('ssn').value=''+selectedSSN;
		document.getElementById('pickList').innerHTML='';
	}


Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Not that this would necessarily solve your problem but what if document.getElementById('ssn').value=[red]''[/red]+selectedSSN; were document.getElementById('ssn').value=[red]' '[/red]+selectedSSN;?

Also, what is returned in users[recno].LicenseeSSN?

_________________
Bob Rashkin
 
Right here:
var users = eval(trim(xmlhttp.responseText));

Your eval function call is taking the string 111-11-1111 after being trimmed, and [red]eval[/red]uating it. In other words its performing the math operation it describes.

Why are you evaluating the string?




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Bong, tried that it still evaluates the string as a number.

Vacunita, It's still in a string after that eval as I'm displaying it in the pick list. I'm evaling because responseText is multiple record/column JSON data from an ajax call.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Bong, sorry I missed this question:

Also, what is returned in users[recno].LicenseeSSN? = 111-11-1111

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Dan, I tried your suggestion and:
Code:
var selectedSSN=[b]String(users[recno].LicenseeSSN)[/b];
						str += '<span onclick="putIn( '+[b]String(selectedSSN)[/b]+' )">'+selectedSSN + " is ";
And it is still performing the subtraction. But it displays normally in:
Code:
document.getElementById('pickList').innerHTML = str;

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Oops - I had the answer last night, but mis-read the quotes in your code so thought you'd already done this:

Code:
<span onclick="putIn([!]\'[/!]' + selectedSSN + '[!]\'[/!])">

Basically, you need to pass it through as a string, not a number. I had thought you were doing this, but mis-counted the quotes.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks Dan that did it. I guess that's what they call escaping[/color red]? I'm going to play with it to try and understand it better.



Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
I guess that's what they call escaping?

Yes and no... Putting backslashes in front of the quotes is escaping the quotes, not the value. The addition of the quotes themselves means the value is passed as a string instead of evaluating it as a number.



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I guess I haven't really understod the use of the escaped quote. Your solution, of course, works great but now I'm trying to introduce a second parmeter to be passed in the putIn() function.

Nothing I try seems to work. I'm also wondering if there is a less complex way of building these dynamic list items.

This code works:
Code:
str += '<li class="listItem" onclick="putIn( \'' +users[recno].LicenseeSSN+ '\')">'+users[recno].LicenseeName+'</li>';

I'm trying to also send, as the second parameter users[recno].LicenseeName.

This is the method I thought should work but it gives me an "unterminated string constant" error.
Code:
str += '<li class="listItem" onclick="putIn( \'' +users[recno].LicenseeSSN+ '\[COLOR=red],[/color]'[COLOR=red]+users[recno].LicenseeName+'[/color])">'+users[recno].LicenseeName+'</li>';

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Code:
str += '<li class="listItem" onclick="putIn(\'' + users[recno].LicenseeSSN + '\[!]'[/!], [!]\'[/!]' + users[recno].LicenseeName + '[!]\'[/!])">' + users[recno].LicenseeName + '</li>';

Basically, every time you want to pass a string value, you need to close the JS string to output the variable, but before doing so, place the opening quote for the string param.

Then, after opening the JS string again, put the closing quote for the string param.



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top