...

View Full Version : Toggle a Form Value w/ jQuery?



twstdelf
12-11-2009, 11:59 PM
Hey there, I'm using the "toggle" method to show/hide a piece of a form. I am trying to update a hidden input field to set a flag whether or not that area should be validated. The element toggle works fine, but I can't get the form value to update the way I would like...

Here's what I have, the initial change (from 0 to 1) works, but it doesn't ever change back to 0 when clicked again...




$(document).ready(function() {
$("#change-pwd-button").click(function(event) {
event.preventDefault();
$("#password-table").toggle("slow");
if ($("#changePassFlag").val("0")) {
$("#changePassFlag").val("1");
} else {
$("#changePassFlag").val("0");
}
});
});



Any help would be appreciated.
Thanks,
TE

A1ien51
12-12-2009, 01:09 AM
Documentation: http://docs.jquery.com/Effects/toggle

2nd parameter is a callback: toggle( speed, [callback] )

You can create an anonymous function and set your field in their.

Eric

twstdelf
12-12-2009, 01:38 AM
Documentation: http://docs.jquery.com/Effects/toggle

2nd parameter is a callback: toggle( speed, [callback] )

You can create an anonymous function and set your field in their.

Eric

Interesting, thanks for the pointer. Unfortunately, it seems to work, but only in the same way it did before, the new code looks like:



$(document).ready(function() {
$("#change-pwd-button").click(function(event) {
event.preventDefault();
$("#password-table").toggle("slow", function() {
if ($("#changePassFlag").val("0")) {
$("#changePassFlag").val("1");
} else {
$("#changePassFlag").val("0");
}
});
});
});


I'm wondering if there's something wrong w/ my logic here:



if ($("#changePassFlag").val("0")) {
$("#changePassFlag").val("1");
} else {
$("#changePassFlag").val("0");
}


... but I can't see what it is...
Ideas?
TE

A1ien51
12-12-2009, 01:44 AM
if ($("#changePassFlag").val("0")) { <-- Error is here
$("#changePassFlag").val("1"); <--
}


You are setting the value, not reading it

Eric

A1ien51
12-12-2009, 01:52 AM
$("#password-table").toggle("slow", function() {
var isOpen = $(this).is(":visible") ? "1" : "0";
$("#changePassFlag").val( isOpen );
});

Eric

twstdelf
12-14-2009, 05:54 PM
$("#password-table").toggle("slow", function() {
var isOpen = $(this).is(":visible") ? "1" : "0";
$("#changePassFlag").val( isOpen );
});

Eric

ahhhh, yes, okay, I see my error now. Thanks!

And that's an awesome rework, thanks so much, appreciate it! :)
Cheers,
TE



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum