Jump to content

Call a JS Function with a a HTML button


IvanConway

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 comment
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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...