Jump to content
IvanConway

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.

Share this post


Link to post
Share on other sites

Its no longer a checkbox so it can't have a checked state attributed to it, you have give a if condition that is true do a copy, maybe check all inputs have a value for instance;

Share this post


Link to post
Share on other sites

I tried editing the if statement in line 2 - if(f.copy.onClick == true){ without success. Could you please give me an example I could experiment with.

Share this post


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'

Share this post


Link to post
Share on other sites

Partly everything, you have lost the clear functionality, which if you had followed my instructions you would have had both.

Share this post


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

Share this post


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

Share this post


Link to post
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

×