Forum Moderators: open

Message Too Old, No Replies

Swapping two input values

         

Jeremy_H

5:53 am on Mar 21, 2006 (gmt 0)

10+ Year Member



Hello,

I'm trying to swap the values of two input boxes with a line of code.

{document.q.b.value=document.q.a.value;
document.q.a.value=document.q.b.value}

Just by looking at it, you might be able to see why it doesnt work.

I need the script to set value B equal to A, and set A equal to the old B, not the new one.

Anybody have any ideas how I might be able to do this?

Thanks

Jeremy_H

6:59 am on Mar 21, 2006 (gmt 0)

10+ Year Member



After trying to solve this problem, I realize the issue lies further down. I wish I could revise or delete my original post, but its too late.

Basically I have a radio box that people can select if they live in the United States or not, and a ZIP code field below where people can type in their ZIP code.

Nothing has been pre-selected and everything is enabled, and I would like to keep it that way.

If the user checks that they live in the US, the ZIP code field remains enabled. However, if they click that they do not live in the US, the ZIP code field becomes disabled.

What I'm trying to do is if they type in their ZIP code first, and then they press that they do not live in the US (by mistake), I want the value of that field to be stored in memory temporarily before the field becomes disabled with a value set to "n/a". That way, if they realize their mistake, and press yes to the US, then the ZIP field becomes active and their previous value returns.

Conversely, if they type in their ZIP code first, and then press Yes to US, then the field remains enabled, and their typed in value stays. But then if they press NO, that value then gets stored temporarily, before the field gets disabled and set to value "n/a". Then if they press YES, the field returns normal.

But here's the thing. It's important to me that if they fill in the field normally, by select Yes first, then typing in their ZIP, but then press NO (or everything vice versa), I need the field to work too.

This is the code I have thus far. I know the issue is kind of confusing, but I've tried everything I could to make this work, but I'm still not there.

Any help would greatly be appreciated.

function dis1(){document.q.q104.disabled=true;var temp=document.q.q104.value;document.q.q104.value="n/a";document.q.q104a.value=temp}
function en1(){document.q.q104.disabled=false;var temp=document.q.q104.value;document.q.q104.value=document.q.q104a.value;document.q.q104a.value=temp}

Fotiman

3:19 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's how I'd do it:


// Global vars
var enVal = "";
var disVal = "n/a";
function dis1()
{
document.q.q104.disabled = true;
// Store whatever zip code was entered
enVal = document.q.q104.value;
// Updated the field with the disabled value
document.q.q104.value = disVal;
}
function en1()
{
document.q.q104.disabled = false;
// Update the field with the enabled value
document.q.q104.value = enVal;
}

Jeremy_H

5:48 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Thanks Fotiman, I didn't realize how to use global variables, so this is a big improvement, thanks.

There's still one bug that I can't seem to work out.

Using that script, if the user types in their ZIP, and then presses yes to enable to field, then the value they just typed in get's erased with var enVal which is blank.

Fotiman

9:19 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Using that script, if the user types in their ZIP, and then presses yes to enable to field, then the value they just typed in get's erased with var enVal which is blank.

Oh, sorry. Here's an updated version.


// Global vars
var enVal = "";
var disVal = "n/a";
function dis1()
{
document.q.q104.disabled = true;
// Store whatever zip code was entered
enVal = document.q.q104.value;
// Updated the field with the disabled value
document.q.q104.value = disVal;
}
function en1()
{
document.q.q104.disabled = false;
// Update the field with the enabled value
if( document.q.q104.value == disVal )
document.q.q104.value = enVal;
}

Jeremy_H

10:16 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Awesome!

Thanks Fotiman, the change is perfect!