Setting single line text value based on another single line text


Badge +10

Hi everybody,

I have two single line text fields. The second text field should be pre-filled (but still editable) with the entry of the first text field. The second text field is only visible if a hook is set. Any ideas how to solve this?

205688_pastedImage_1.png

205689_pastedImage_2.png

Cheers,

mai-kel


10 replies

Badge +9

Hi Michael Weiß,

Use JS code. Hope you know where to add JS code and how to name field controls.

NWF$(document).ready(function(){  
  fnChangeCheckBox();
NWF$('#' + varCheckBox).click(function()
{
 fnChangeCheckBox();
});
   NWF$('#' + varField2).hide();
});
function fnChangeCheckBox()
{
  var checkBox = NWF$('#' + varCheckBox);
  if (checkBox.is(':checked') == true){
  var field1 = NWF$('#' + varField1).val();
  NWF$('#' + varField2).val(field1);
  NWF$('#' + varField2).show();

  }
  else
  {
  var nullValue = '';
  NWF$('#' + varField2).val(nullValue);
  NWF$('#' + varField2).hide();
  }
}

Thanks,

Lakshmi C

Badge +10

Hi Lakshmi C,

First of all, thanks for your response. But unfortunately this isn't working here.

I named the first single line textbox:

205694_pastedImage_1.png

I named the second single line textbox:

205695_pastedImage_2.png

AnD I also named the checkbox:

205696_pastedImage_3.png

Then I copied your text/code into the Custom JavaScript section. No success! sad.png

Badge +9

Enter some text into field1 and check the check box. It will copy it to field2.

205683_pastedImage_1.png

Badge +10

OK, I tried it with a blank form. There is works. Thanks a lot!

Badge +10

Hi Lakshmi C,

I had to alter my form a little bit: instead of only having txtbox 2 visible/not visible via checkbox I had to do this also for txtbox 1. And now your solution is not working in every case.

Situation looks like this:

Start:

205780_pastedImage_1.png

Step 1:

205814_pastedImage_2.png

Step 2:

205815_pastedImage_3.png

Step 3: (here the script is working TEXT is copied to txtbox 2):

205816_pastedImage_4.png

Step 4: Everything fine so far. I could also do this, no problem (I typed in TEXT, no copy function):

205817_pastedImage_5.png

Saving Step 4 is also no problem. I can go into view mode everything is fine, BUT if I want to edit Step 4 it looks like this:

205818_pastedImage_6.png

txtbox 2 has vanished. It is simply not there anymore in edit mode. When I switch back to view mode it is there again.

WTF?

Badge +9

Hi ‌,

Now you have two check boxes, so you have to write two separate functions to achieve your requirement. You didn't put your code. See below my code, this will work.


NWF$(document).ready(function()
{  
  fnChangeCheckBox1();
  var field2Val = NWF$('#' + varField2).val();
  if(field2Val == '')
{
 NWF$('#' + varField2).hide();
}
 NWF$('#' + varCheckBox1).click(function()
{
 fnChangeCheckBox1();
});
NWF$('#' + varCheckBox2).click(function()
{
 fnChangeCheckBox2();
});
});
function fnChangeCheckBox1()
{
var checkBox = NWF$('#' + varCheckBox1);
  if (checkBox.is(':checked') == true){
NWF$('#' + varField1).show();
 }
 else
 {
   NWF$('#' + varField1).hide();
 }
}
 function fnChangeCheckBox2()
{
  var checkBox = NWF$('#' + varCheckBox2);
  if (checkBox.is(':checked') == true){
  var field1 = NWF$('#' + varField1).val();
  NWF$('#' + varField2).val(field1);
  NWF$('#' + varField2).show();
  }
  else
  {
  var nullValue = '';
  NWF$('#' + varField2).val(nullValue);
  NWF$('#' + varField2).hide();
  }
}

205859_pastedImage_1.png

Thanks,

Lakshmi C

Badge +10

Works perfecty fine (again!). Thanks a lot!

Cheers

mai-kel

Userlevel 4
Badge +7

Hi

Don't forget to mark ‌ answer as correct to help others find the solution

Badge +10

Hi Paul, how can I do that in this case? Because I have already marked his first response "correct"? In fact there were two different questions with two different (correct) answers....

Userlevel 4
Badge +7

Hi

Don't worry, I think you did it as I was writing my last reply! My screen refreshed and I saw you had done it! I was too quick responding

Reply