Jump to content

Call a JS Function with a a HTML button


Recommended Posts

I am trying to copy form inputs from 3 address fields to corresponding duplicate fields in another section of my form. If I use the following script and html the process works perfectly.

<input type="checkbox" id="copy" value=""  name="copy" onclick="CopyAddress(this.form)">

 <script>

function CopyAddress(f) {
  if(f.copy.checked == true) {
    f.bstr.value = f.gstr.value;
    f.bsubcit.value = f.gsubcit.value;
    f.bstate.value = f.gstate.value;
    f.bpcode.value = f.gpcode.value;
}
else {
    f.bstr.value = "";
    f.bsubcit.value = "";
    f.bstate.value = "";
    f.bpcode.value = "";}
}

<script>

I would like to change the checkbox to a button like so:

<input type="button" id="copy" value="Copy" name="copy" onclick="CopyAddress(this.form)">

However once I change to a button, the script will not run and I can't work out why.  My limited understanding tells me the fault lies in line 2 of the JS,  if(f.copy.checked == true).

Can anyone tell me where I am going wrong please.

Link to post
Share on other sites

What i think you want is for the value of input to equal 'copy' so the if condition can check for this value.

if(f.copy.value === 'copy') {....}

Then when the values are copied over, change value to 'clear'.

f.copy.value === 'clear'

When the inputs are cleared reset to

f.copy.value === 'copy'

Link to post
Share on other sites

My thanks to you dsonesuk for your advice and help.  My reason for not following your instructions in your post was due mainly to my limited understanding of exactly how to do itcorrectly to achieve the result that I wanted.  I apologise to you for my age and lack of understanding.  Ivan

Link to post
Share on other sites
On 3/27/2018 at 3:00 PM, IvanConway said:

My thanks to you dsonesuk for your advice and help.  My reason for not following your instructions in your post was due mainly to my limited understanding of exactly how to do itcorrectly to achieve the result that I wanted.  I apologise to you for my age and lack of understanding.  Ivan

That's fine I guess, Being new to programming it is obvious,

I am also having same issues:D..... 

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.

Guest
Reply to this topic...

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