+ Reply to Thread
Results 1 to 4 of 4

Thread: Problem with adding value to form textbox using jQuery

  1. #1
    jason is offline Coder jason will become famous soon enough
    Join Date
    Apr 2008
    Posts
    70

    Default Problem with adding value to form textbox using jQuery

    I am going out of my mind with what I would typically believe to be a trivial problem...

    I have a page with a form. In the form I have a selection box and a few text boxes. When I change the selection box value to zero it should clear the text boxes, and when I change the selection box to any other value it sets the text boxes to other values (which it gets from an ajax call). This all works fine using the following code which I call from my jQuery document.ready event handler.

    Code:
    if($("select#myselboxID").val() != 0){ // if my selection box value is not zero
                
                    $.ajax({ type: "POST", url: "includes/ajax/getDetails.php?id=" + $("select#myselboxID").val(), dataType: "xml", success: function(xml){                    
                        $("form input#name").attr("Value", $(xml).find('name').text());                     
                        $("form input#phone").attr("Value", $(xml).find('phone').text());
                        $("form input#email").attr("Value", $(xml).find('email').text());
                        $("form textarea#_address").text($(xml).find('address').text());
                    }});
                }
                else{ // if my selection box value is zero
                    $("form input#name").attr("Value", "");                     
                    $("form input#phone").attr("Value", "");
                    $("form input#email").attr("Value", "");
                    $("form textarea#address").text("");
                }
    
    So with the above I can make my form values clear and change to other values etc to my hearts content. However if I go down and manually edit a text box value and then change the value in the selection box, the value I manually set the textbox to stays there and only the other non edited textbox fields change. When really I want all textbox entries to change regardless of whether or not I manually modified them.

    Could someone please explain to me why this is happening? I feel like there is something significant about how a browser works with form values that I'm clearly unaware of.

    Many Thanks

  2. #2
    davkell is offline Coder davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of
    Join Date
    Jul 2007
    Location
    Galway
    Posts
    89

    Default

    I have something that's doing something similar (but not exactly the same). I use:

    Code:
        $('.display_reply').click(function(){ // if a button is clicked
             $('textarea, :text').val('');       // empty a text area & all text boxes
        });
    
    That will clear everything in my form (1 textarea & 3 input text boxes) when a button with that class is clicked.

    Have you tried using .val() instead of setting the .attr() ?

    Also, how is the code you posted being triggered? Could it be to do with the event you're using when the select box is changed?
    Not Work: My blog
    Work: Website Design Galway
    A little app: Twiteye.com (Application, software & service ideas from Twitter)

  3. #3
    jason is offline Coder jason will become famous soon enough
    Join Date
    Apr 2008
    Posts
    70

    Default

    Cheers davkell for your reply

    how is the code you posted being triggered?
    Sorry I should have mentioned that the code fires on the selection box change event. This event does fire at the appropriate times, which I have proven to myself by simply inserting alert() calls in the code block.

    I just tried using val() to init the values of the form components and it worked a charm!! - fair play

    So there's a difference between using val("") and .attr("Value", "") it would appear. At one level they work the same and then on another, they don't.

    All the best

  4. #4
    davkell is offline Coder davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of davkell has much to be proud of
    Join Date
    Jul 2007
    Location
    Galway
    Posts
    89

    Default

    No bother, good to see it worked for you!
    Not Work: My blog
    Work: Website Design Galway
    A little app: Twiteye.com (Application, software & service ideas from Twitter)

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 01-07-2008, 02:07 PM
  2. Replies: 11
    Last Post: 13-04-2007, 04:45 PM
  3. Drupal, problem with adding content by a privilaged user
    By MacKozer in forum CMS and Content Management
    Replies: 1
    Last Post: 11-04-2007, 11:06 AM

Tags for this Thread

Posting Permissions

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

Search Engine Optimization by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53