nic_dk
nic_dk

Reputation: 1

Combining two input fields to one, jquery

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>

Upvotes: 0

Views: 2053

Answers (3)

Haroldo
Haroldo

Reputation: 37377

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

Upvotes: 1

Matt Ellen
Matt Ellen

Reputation: 11592

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

Upvotes: 0

Haroldo
Haroldo

Reputation: 37377

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

Upvotes: 2

Related Questions