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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

    JQUERY reset one field not each

    Hi,

    Once my form has been inserted into my database I want to reset only one field (the rest all have hidden information I want to keep). At the moment I have:
    Code:
    	$.post ('insert.php',data, function(){
    			$(Frm).each (function(){
    				this.reset();
    But what I want to do is just reset my message field in the Frm. I tried $(Frm).message but that didn't work.

    I'm quite happy to look this up myself but googling I haven't been able to find the right term to find my answer. I'm guessing the .each is the problem but as I said I have tried to google and not knowing what I am looking for I'm going round in circles.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Why use javascript?

    say the id of the message element is txtMsg,

    Code:
    document.getElementById('txtMsg').value = '';

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    It is part of several forms on the same page that all have mulitple values that need to be sent to my insert page to be added to my database and I don't want to redirect. I tried several measures and JQuery in general worked best for me. But in the form four fields contain hidden values references/IDs etc so I don't want to reset them.

    It's basically one of several talk rooms that can run simultaneously, but the forms are called to the page as needed with a maximum of five (so there are five versions of the PHP script which can all be called to the main page - this was for displaying purposes). So the whole code goes:
    Code:
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function (){
    	$('#submitchat1, #submitchat2, #submitchat3, #submitchat4, #submitchat5').live('click',function(){
    
    
    				var btn = $(this).attr('id');
    				var Frm;
    				if(btn == 'submitchat1'){
    				Frm = 'chatmessage1';
    				} else if(btn == 'submitchat2'){
    				Frm = 'chatmessage2';
    				} else if(btn == 'submitchat3'){
    				Frm = 'chatmessage3';
    				} else if(btn == 'submitchat4'){
    				Frm = 'chatmessage4';
    				} else if(btn == 'submitchat5'){
    				Frm = 'chatmessage5';
    				} 
    
    	var data = $(Frm).serialize();
    	$.post ('insertChat.php',data, function(){
    				alert("called insert");
    			$(Frm).each (function(){
    				alert("called reset");
    				this.reset();
    			});	
    				return false;
    		});
    	});
    });
    </script>
    PHP Code:
    <?php
    session_start
    ();

        if (!(isset(
    $_SESSION['login']) && $_SESSION['login'] !='')){
            
    header ("Location: Welcome.php");
            }
    $SMyPId=$_SESSION['MyPId'];

    include(
    "dbconnect.php");

        
    $blossom=explode(';',$_POST['artemis']);
        
    $periwinkle=$blossom[0];
        
    $peppermint=$blossom[1];
        
    $pawpaw=$blossom[2];
        
    $pine=$blossom[3];

        echo 
    '<div id="displaytalkTwo"></div>';

        echo 
    '<br /><br /><br /><br />';
        echo 
    '<form action="" method="post" name="chatmessage2" id="chatmessage2">
            <input type="text" class="hidden" name="from" id="from" value="' 
    $SMyPId '">
            <input type="text" class="hidden" name="to" id="to" value="'
    ;
            echo (
    $SMyPId==$peppermint) ? $periwinkle $peppermint '">
            <input type="text" class="hidden" name="ref" id="ref" value="' 
    $pine '">
            <input type="text" class="hidden" name="chatbox" id="chatbox" value="chat2">'
    ;

        echo 
    '<div class="textchat">';     
        echo 
    '<textarea cols="18" row="5" name="message" id="message"></textarea>';
        echo 
    '<input type="button" name="submitchat2" id="submitchat2" value="&nbsp;"></div></form>';
        
    ?>
    Last edited by Jenny Dithe; 03-30-2011 at 05:33 PM.

  • #4
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    Code:
    $(Frm).each (function(){
    				alert("called reset");
    				this.reset();
    			});
    I think the problem is that this here will refer to the form, so everything gets reset.

    Try:

    Code:
    $(Frm).each (function(){
    				alert("called reset");
    				$(this).find('#message').reset();
    			});

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Thank you for the tip unfortunately that stalls the process.

  • #6
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    Hm. Taken another look at this and I think your Frm variable isn't quite right because it is for example 'chatmessage2'. You're then using that as a jQuery selector which won't find anything because the selector should be #chatmessage2.

    So...

    Code:
    <script type="text/javascript">
    $(document).ready(function (){
    	$('#submitchat1, #submitchat2, #submitchat3, #submitchat4, #submitchat5').live('click',function(){
    
    				var Frm = $(this).parent();
    				var data = $(Frm).serialize();
    	$.post ('insertChat.php',data, function(){
    				alert("called insert");
    				$(this).siblings('#message').val('');
    				return false;
    		});
    	});
    });
    </script>

  • Users who have thanked SB65 for this post:

    Jenny Dithe (04-01-2011)

  • #7
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Thanks. I managed to get it working I think.


  •  

    Posting Permissions

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