0

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>

3 Answers 3

2

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)); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

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);       
});

4 Comments

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 :(
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>
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()
textareas are different to inputs. the .val() method wont work instead try, .text() or .html()
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.