Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
simple jscript form copy function issue
Old 01-16-2012, 07:03 PM simple jscript form copy function issue
Skilled Talker

Posts: 69
Name: Greg
Location: South Carolina
Trades: 0
Well I feel like an idiot, because its simple and I cant get it to work I am a jscript newb..... I have a form with billing and shipping addresses. If they choose a country other than USA then it replaces the billing state field with the billing alternate state field. I then try to check that in the jscript function I created to copy the billing address to shipping address if they click the copy address function.

Below you see the bstate_value code which is obviously wrong, the odd thing is... it will copy the alternate state values when I have it like this. Syntactical error I caught... Anyways if I fix that to be f.bstate.value || etc it will for the bstate but not alt state.

If this is confusing I can put in the code that I think should work but is not.

form fields are

bstate, sstate, balt_state, salt_state

Code:
function cbilling(f) {
  if(f.usebilling.checked == true) {		  
    f.saddr1.value = f.baddr1.value;
	f.scity.value = f.bcity.value;
	f.szipc.value = f.bzipc.value;
	if (!f.bstate_value || 0 === f.bstate_value.length){
	  f.salt_state.value = f.balt_state.value;
	} else {
		f.sstate.value = f.bstate.value;
	}
	
  }
}
thanks in advance
scuts is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-17-2012, 05:41 AM Re: simple jscript form copy function issue
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Well I feel like an idiot,
Hang on, I'll see if I can find you one. We do have a few to spare most days.

Your logic on this !f.bstate_value may be a little flawed, depending on where the value comes from, checking for to be true || false may not give the results you are expecting.

http://www.mapbender.org/JavaScript_...undefined,_NaN
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-17-2012, 06:33 AM Re: simple jscript form copy function issue
Skilled Talker

Posts: 69
Name: Greg
Location: South Carolina
Trades: 0
I needed that laugh this morning.

Code:
if (!f.bstate_value || 0 === f.bstate_value.length){
needs to be this

Code:
if (!f.bstate.value || 0 === f.bstate.value.length){
These are form elements and in the case that someone sets the country to a non US country I refresh the form and remove bstate swapping it out for free form balt_state so that the user can put in their own value

In the jscript I am then trying to say, if there is no value in bstate then use balt_state to copy to salt_state

Based on the link you sent, I would have thought that the === would have caught an undefined variable in this case
scuts is offline
Reply With Quote
View Public Profile
 
Old 01-17-2012, 06:54 AM Re: simple jscript form copy function issue
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
yep but the === is applied to the length so if there was even a space in the field it would be true.

it is the "if NOT f.bstate.value" where the error is, you are using a string value as a boolean which doesn't always work. I could put true in that field and your logic would accept it, because length would be greater than zero and the value would NOT be false.

If you are checking for an empty string then check for ''
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-17-2012, 07:11 AM Re: simple jscript form copy function issue
Skilled Talker

Posts: 69
Name: Greg
Location: South Carolina
Trades: 0
in all actuality the field wont even be on the form when this function is called. I am removing it with php based on their choice of country. If USA there is a bstate field else its balt_state. Is checking for an empty string the proper way to handle that?
scuts is offline
Reply With Quote
View Public Profile
 
Old 01-17-2012, 09:24 AM Re: simple jscript form copy function issue
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
It it doesn't exist in the form checking for a value will throw an exception.
use
Code:
if (document.getElementById(id) != null)
to test if the element actually exists before trying to get the value.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-17-2012, 11:10 AM Re: simple jscript form copy function issue
Skilled Talker

Posts: 69
Name: Greg
Location: South Carolina
Trades: 0
got it. thanks a ton for the help.
scuts is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to simple jscript form copy function issue
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33664 seconds with 12 queries