Irish SEO,  Marketing & Webmaster Discussion

 

Problem with adding value to form textbox using jQuery

This is a discussion on Problem with adding value to form textbox using jQuery within the Coding Help forums, part of the Webmaster Help category; I am going out of my mind with what I would typically believe to be a trivial problem... I have ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Calendar Search Today's Posts Mark Forums Read

  #1 (permalink)  
Old 21-10-2008, 03:57 PM
Coder
 
Join Date: Apr 2008
Posts: 57
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
jason will become famous soon enough
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 21-10-2008, 10:16 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 85
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 22-10-2008, 12:06 PM
Coder
 
Join Date: Apr 2008
Posts: 57
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
jason will become famous soon enough
Default

Cheers davkell for your reply

Quote:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 22-10-2008, 12:07 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 85
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

No bother, good to see it worked for you!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
adding, form, jquery, problem, textbox

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Is this possible? (Ajax & JQuery Document Scope Problem) jason Coding Help 4 01-07-2008 02:07 PM
Can anyone recommend a text editor (textbox) like TinyMCE for websites? dude Webmaster Discussion 11 13-04-2007 04:45 PM
Drupal, problem with adding content by a privilaged user MacKozer CMS and Content Management 1 11-04-2007 11:06 AM


Sponsored links

Pepperjam Network
Paid On Results www.zanox.com


All times are GMT +1. The time now is 01:45 PM.


Powered by: vBulletin Version 3.8.2, Copyright ©2000 - 2009, Jelsoft Enterprises Limited.
Hosted in Ireland by Blacknight - Test your ISP |Irish Hosting Directory| Armchair.ie|Logo by Eden Web Design|Avatars by Afterglow |Latest Blog Entries | VPS HostingAd Management by RedTyger

Search Engine Friendly URLs by vBSEO 3.3.0

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