...

View Full Version : JQUERY reset one field not each



Jenny Dithe
03-30-2011, 05:27 AM
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:


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

bullant
03-30-2011, 05:57 AM
Why use javascript?

say the id of the message element is txtMsg,


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

Jenny Dithe
03-30-2011, 05:30 PM
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:



<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
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>';

?>

SB65
03-30-2011, 07:11 PM
$(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:


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

Jenny Dithe
03-30-2011, 08:48 PM
Thank you for the tip unfortunately that stalls the process.

SB65
03-30-2011, 09:19 PM
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...


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

Jenny Dithe
03-30-2011, 09:47 PM
Thanks. I managed to get it working I think.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum