Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2008
    Location
    Los Angeles
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Toggle a Form Value w/ jQuery?

    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...

    Code:
    $(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
    Last edited by twstdelf; 12-12-2009 at 01:53 AM.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Oct 2008
    Location
    Los Angeles
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51 View Post
    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:

    Code:
    $(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:

    Code:
    if ($("#changePassFlag").val("0")) {
    	$("#changePassFlag").val("1");
    } else {
    	$("#changePassFlag").val("0");
    }
    ... but I can't see what it is...
    Ideas?
    TE

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    if ($("#changePassFlag").val("0")) {  <-- Error is here
        $("#changePassFlag").val("1"); <-- 
    }
    You are setting the value, not reading it

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    $("#password-table").toggle("slow", function() {
        var isOpen = $(this).is(":visible") ? "1" : "0";
        $("#changePassFlag").val( isOpen );
    });
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    New Coder
    Join Date
    Oct 2008
    Location
    Los Angeles
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51 View Post
    Code:
    $("#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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •