2

I have three input text boxes in my form.

First one is teacher, second one is grade. Now I've the third input text box, where i need to update the value dynamically from these two values as "(teacher)'s (X) class" using JavaScript.

Can anyone help me in writing the code? Thanks in advance.

here is the jquery code:

<script type="text/javascript">
  $(document).ready(function(){
    $('#both').value($('#teacher').val() + "'s " + $('#grade').val());
});

  </script>

and my input tags are:

<input type="text" name="teacher_name" id="teacher" value="" style="background:#f7f6f2 url(../images/input_02.png) repeat left top;border: 0px;width: 250px;height: 10px;padding: 10px;display: block;">
<input type="text" name="teacher_name" id="grade" value="" style="background:#f7f6f2 url(../images/input_02.png) repeat left top;border: 0px;width: 250px;height: 10px;padding: 10px;display: block;">
<input type="text" name="teacher_name" id="both" value="" style="background:#f7f6f2 url(../images/input_02.png) repeat left top;border: 0px;width: 250px;height: 10px;padding: 10px;display: block;">

what are the things i need to add to make my third input field to get updated.

1
  • 1
    Can you show us what you have so far? Commented Jun 23, 2012 at 6:22

4 Answers 4

3

Use the onChange event that is triggered when an input content changes or the onBlur event that is triggered when an input lost the focus:

function UpdateInfo()
{
  var teacher = document.getElementById('teacher').val();
  var grade = document.getElementById('grade').val();
  var info = teacher + '\'s '+ grade + ' class';
  document.getElementById('info').value = info;
}

/* Solution 1 (onChange) */
<input id="teacher" type="text" onChange="UpdateInfo();" />
<input id="grade" type="text" onChange="UpdateInfo();" />
<input id="info" type="text" />

/* Solution 2 (onBlur) */
<input id="teacher" type="text" onBlur="UpdateInfo();" />
<input id="grade" type="text" onBlur="UpdateInfo();" />
<input id="info" type="text" />

Or using jQuery, you can bind the event at document.ready event:

$(document).ready(function()
{
    /*
    * binding onChange event here
    * you can replace .change with .blur
    */
    $('#teacher').change(UpdateInfo);
    $('#grade').change(UpdateInfo);
});

   function UpdateInfo()
   {
     var teacher = $('#teacher').val();
     var grade = $('#grade').val();
     var info = teacher + '\'s '+ grade + ' class';
     $('#info').val(info);
   }

<input id="teacher" type="text" />
<input id="grade" type="text" />
<input id="info" type="text" />

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

no this one is also not working seems everything is fine but in general page also this code is not working
i thought this will work fine but not working in my code don't know what the fault i'm doing i just updated these in my code but my input fields are not changing....
1

If you are using jQuery it's pretty simple to update the third one when the dom is ready:

$(document).ready(function(){
    $('#third').value($('#first').val() + "' " + $('#second').val());
});

3 Comments

do you have jquery included on your page?
what if the values are passing dynamically???jsfiddle.net/srinivaswaterdrop01/PUbKj see this code
what do you mean by "dynamically"? no one will help you figure it out if you don't explain what you want.
0

With javascript, you can try this method:

var first_val=document.getElementById('first').val();

var second_val=document.getElementById('second').val();

document.getElementById('third').val(first_val + second_val);

Comments

0

I think the <output> element is what you're looking for. The example on MDN covers this use case exactly.

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.