0

I'm trying to get values form fields on checking the check box, basically what i'm trying to do is when ever user clicks the check box it should take data from input fields and copies that to the next fields.
here is my code:

jQuery(document).ready(function(){

jQuery('#check_1').change(function() {
        if(jQuery(this).is(":checked")) {
          
var address    = jQuery('#address').attr('value');
var address_2  = jQuery('#address2').attr('value');

alert('Copy selection');
alert(address);
alert(address_2);

$('#cpy_1').val(address);
$('#cpy_2').val(address_2);

 
}
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' name='address' id="address"/>
<input type='text' name='address2' id='address2' />

<br>
<br>
<hr>
<br>
<h4>Data copied</h4>

<input type='checkbox' name='check_1' id="check_1"/>Copy above fields
<input type='text' name='cpy_1' id='1cpy_1'/>

<input type='text' name='cpy_2' id='cpy_2'/>

but i'm getting undefined by getting input text's value. Please tell me what's wrong there ?

2 Answers 2

1

Change like this :

$(document).ready(function() {
    
    $('#check_1').change(function() {
        
        if($(this).is(":checked")) {
            
            var address    = jQuery('#address').val();
            var address_2  = jQuery('#address2').val();
            
            $("#1cpy_1").val(address);
            $("#cpy_2").val(address_2);

        }
        
    })
    
})
  <input type='text' name='address' id="address"/>
<input type='text' name='address2' id='address2' />

<br>
<br>
<hr>
<br>
<h4>Data copied</h4>

<input type='checkbox' name='check_1' id="check_1"/>Copy above fields
<input type='text' name='cpy_1' id='1cpy_1'/>

<input type='text' name='cpy_2' id='cpy_2'/>
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

and how do we assign these values to some other fields ?
yea sure but there are restrictions of marking it before 5 minutes
1

Try this:

var address    = jQuery('#address').val();
var address_2  = jQuery('#address2').val();

but i'm getting undefined by getting input text's value

It's because your inputs doesn't have attributes value:

<input type='text' name='address' id="address"/>
<input type='text' name='address2' id='address2' />

You have type, name and id without value.

3 Comments

and how do we assign these values to some other fields ?
Example. Set the value of the <input> field (link ): $("button").click(function(){ $("input:text").val("Glenn Quagmire"); });
okay i got it: jQuery('#cpy_1').val(address); jQuery('#cpy_2').val(address_2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.