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
Can not remove value of a field using a variable yet I can use document.x.y.z
Old 07-06-2010, 08:25 PM Can not remove value of a field using a variable yet I can use document.x.y.z
Experienced Talker

Posts: 37
Trades: 0
I have a form where I have it set onblur a first password field it is to remove the value data from the password2 field. It's set up initially with:

PHP Code:
regForm document.registerForm;
regPwd regForm.regPwd.value;
regPwd2 regForm.regPwd2.value
I have an onblur to each field in the form to run a function. In the function is a switch/case routine to detect which field was just left and it runs validation on each. For the 1st password field it begins like this:

PHP Code:
case "regPwd":
     
regPwd2 ""
This does not work! I have even changed it to alert (regPwd2) and it shows the contents of the second password field, so I know it's set properly. However, if I change it to:

PHP Code:
case "regPwd":
            
document.registerForm.regPwd2.value ""
It works as expected, and when I leave the first password field the data is erased from the second password field.

Is this a bug, or does anyone understand why it behaves this way? Is there a restriction to changing the DOM via Variable assignment? I thought when you assign a variable to a DOM location such as document.x.y.z, that this is a pointer to the DOM and it could be changed as such... But I am thinking now that it must be a local copy to the variable and doesn't not change the DOM directly.

Is this correct?
catatung is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-07-2010, 08:35 AM Re: Can not remove value of a field using a variable yet I can use document.x.y.z
Extreme Talker

Posts: 246
Trades: 0
This is my understanding of what you were trying to do.

You initially put the value of the textbox into a variable and then cleared the variable. Since it is a string which is a value type and not a reference type it copies the value into the variable. You then cleared the contents of the variable not the value in the textbox.

From what I can tell you thought that regPwd2 = regForm.regPwd2.value; would have given you a pointer (reference) to the value in the textbox rather than the value itself, which is wrong.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 07-07-2010, 07:37 PM Re: Can not remove value of a field using a variable yet I can use document.x.y.z
Experienced Talker

Posts: 37
Trades: 0
That just about sums it up... That's what I thought initially and when it wasn't working my hunch was what you said, that it was just a copy and not a pointer reference.

I just wanted to confirm with someone who would know from experience.

Thanks!
catatung is offline
Reply With Quote
View Public Profile
 
Old 07-07-2010, 07:40 PM Re: Can not remove value of a field using a variable yet I can use document.x.y.z
Experienced Talker

Posts: 37
Trades: 0
Thinking further, I'm assuming I could have done something like

regPwd2 = new Object();

But I think it would just be a copy of the object. Is there a way to link a new object to another object where changing one would change both? I don't need to (at this point), but just out of curiosity thought I would ask.
catatung is offline
Reply With Quote
View Public Profile
 
Old 07-08-2010, 08:10 AM Re: Can not remove value of a field using a variable yet I can use document.x.y.z
Extreme Talker

Posts: 246
Trades: 0
Objects in Javascript are reference types (pointers) such as String(), Date(), Array(), Number() and CustomObject() (There are more as well).

If you REALLY want to know the inner workings of JavaScript get "JavaScript, The Definitive Guide" by David Flanagan it has more information than you might think in it. Right now I'm only on chapter 6 and I've learned quite a bit yet I've been using it for years and never REALLY learned it.


So to your question, look at the following:

var dateOne = new Date(2010, 6, 31); //new Date Object

var dateTwo = dateOne;

dateOne.setDate(12);

dateTwo will now also be July 12, 2010.

If you change the date for dateOne it will affect dateTwo and vice versa.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Can not remove value of a field using a variable yet I can use document.x.y.z
 

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.28359 seconds with 12 queries