IvanConway Posted March 23, 2018 Share Posted March 23, 2018 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 More sharing options...
dsonesuk Posted March 23, 2018 Share Posted March 23, 2018 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; Link to comment Share on other sites More sharing options...
IvanConway Posted March 23, 2018 Author Share Posted March 23, 2018 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. Link to comment Share on other sites More sharing options...
dsonesuk Posted March 23, 2018 Share Posted March 23, 2018 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 More sharing options...
IvanConway Posted March 24, 2018 Author Share Posted March 24, 2018 Thank you for your help. However I have achieved exactly what I want to do with the attached html file. Button Copy.html Link to comment Share on other sites More sharing options...
dsonesuk Posted March 24, 2018 Share Posted March 24, 2018 Partly everything, you have lost the clear functionality, which if you had followed my instructions you would have had both. Link to comment Share on other sites More sharing options...
IvanConway Posted March 27, 2018 Author Share Posted March 27, 2018 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 More sharing options...
Warriorofwarez Posted March 29, 2018 Share Posted March 29, 2018 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..... Link to comment Share on other sites More sharing options...
IvanConway Posted March 29, 2018 Author Share Posted March 29, 2018 I wish I had started 20 years ago. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now