Jump to content
  • 0

Copy value in one field to another when checkbox is clicked


Question

I need assistance in implementing a JS  when a check box is ticked.  I have 2 address fields (Permanent and Present), and a  "Same as" Checkbox field.  I want to automatically copy the value of "Present address" field to "Permanent Address" field when the "Same as" Checkbox field.

 

 

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Hello @NeoInJS,

 

We would need to reference to your DOM in your webpage so we can refer to them in our JS. The current markup convention of Caspio in form fields is as follows:

Virtual fields = cbParamVirtual and it's occurence number (e.g. cbParamVirtual 1, cbParamVirtual2)

Submission Form DataPage = 'InsertRecord' and field_name (e.g. InsertRecordPresentAddress, InsertRecordPermanentAddress)

Edit Details (Reports or Details DataPage) = 'EditRecord' and field_name (e.g. EditRecordPresentAddress, EditRecordPermanentAddress)

 

I just assumed you are using one of the two DataPages. To implement this, you would need to paste this script in the footer and replace your field names accordingly

<script>

var presentAddr = document.getElementById('InsertRecordPresentAddress');
var permaAddr = document.getElementById("EditRecordPermanentAddress");
var chkBox = document.getElementById("cbParamVirtual2");

chkBox.onclick = function() {
presentAddr.value = permaAddr.value;
}

</script>

 

Link to post
Share on other sites
  • 0

Just to add up, you can do a neat functionality to remove the value if the checkbox is unchecked. See updated code:
 

 

<script>

var presentAddr = document.getElementById('InsertRecordPresentAddress');
var permaAddr = document.getElementById("EditRecordPermanentAddress");
var chkBox = document.getElementById("cbParamVirtual2");

if (presentAddr.value == permaAddr.value) chkbox.checked = true;

chkBox.onclick = function() {
if (chkBox.checked)
presentAddr.value = permaAddr.value;
else 
presentAddr.value = '';
}

</script>
Link to post
Share on other sites
  • 0

@NeoInJS

 

You can try this code in the footer:

<script>
function populateText()
{
 if(document.getElementById('chechboxid').checked)
 {
  var str=document.getElementById('textbox1id').value;// textbox in which you enter data
  document.getElementById('textbox2id').Value= str;
}
}


//call the javascript function as attribute to checkbox in pageload method of your .aspx page
pageload()
{
 checkboxid.attributes.add("onclick","populateText();");
}
</script>

Link to post
Share on other sites
  • 0

Hi @NeoInJS,

Another way to do this without using JavaScript is by using triggers. If you do not have triggers in your plan, please ignore this idea.

First in the submission form, create a Rule which hides the Permanent Address field. If the checkbox is checked, then show it and the user enters the address. If not, it will be hidden.

If the checkbox field is not checked, the trigger will execute. Create a trigger on this table which will update the inserted record with "Permanent Address = Present Address" only when the checkbox is checked.

Hope this helps.

Regards.

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...