tags:

views:

100

answers:

3

Im trying to combine the name field, and msg field, and input all values into #msg, but cant quite get it to work

<script type="text/javascript" language="text/javascript">
  $('#DocumentCommentsForm_21').bind('submit', function(){
    var name = "##" + $('#navn').val() + "##";
    var msg = $('#msg').val();
    $('#msg').val(name+' '+msg);
  });
  alert($('#msg').val(name+' '+msg));
 </script>
+1  A: 

you need to get your alert inside the function:

$('#DocumentCommentsForm_21').bind('submit', function(){
    var name = "##" + $('#navn').val() + "##";
    var msg = $('#msg').val();
    $('#msg').val(name+' '+msg);
    alert($('#msg').val(name+' '+msg)); 
});
Haroldo
+1  A: 

if #msg is not an input, use .text() instead of .val();

$('#DocumentCommentsForm_21').bind('submit', function(){
    var name = "##" + $('#navn').val() + "##";
    var msg = $('#msg').val();
    $('#msg').text(name+' '+msg);       
});
Haroldo
Still doensn't work :( It's like it doesnt register that the form is submitted - Cant get an alert inside $('#DocumentCommentsForm_21').bind('submit', function(){ to work :(
nic_dk
the form looks like: The form looks like this: <form id="DocumentCommentsForm_21" method="post" action=""> <input type="hidden" name="Action" value="Post" /> <input type="hidden" name="DCID" value="21" /> <input type="hidden" name="CommentId" value="-1" /> <label for="navn">Navn:</label> <br /> <input type="text" name="navn" value="" id="navn" /> <br /> <br /> <textarea cols="30" rows="10" id="msg" name="DocumentComment">(Your comment here)</textarea></form>
nic_dk
if the form or submit button is not on the page when the page loads (in an ajax tab etc) use .live() instead of .bind()
Haroldo
textareas are different to inputs. the .val() method wont work instead try, .text() or .html()
Haroldo
A: 

You are putting the "combine fields" functionality into the submit event handler, which is fine, but you don't stop the form from submitting, so you will never see the result of you operation on the original form. If this is your intention, and you just want the alert to show your combined result then Haroldo's approach would pretty much suffice except that you'll want to change the code to this:

$('#DocumentCommentsForm_21').bind('submit', function(){
    var name = "##" + $('#navn').val() + "##";
    var msg = $('#msg').val();
    $('#msg').val(name+' '+msg);
   alert($('#msg').val()); 
});

Otherwise you'll get an alert box saying [object Object].

Matt Ellen