admin管理员组

文章数量:1325137

I have two inputs(number).

How can I duplicate data from first to second and back?

For example, I'll set some value in first input and in second input I get same value and if I set same value in second input I want to get same value in first input.

I think it must be something like that

<div class="first">
    <input type="text" id="email">
</div>  
<div class="second">
    <input type="text" id="name">
</div>
<div id="slider"></div>

<script>
$('#email').change(function(){
    $(this).val($('#name').val());
});
('#name').change(function(){
    $(this).val($('#email').val());
});    
$('#slider').slider();
</script>

/

When I'm trying to put smth in slider() function I get error in console (slider function doesn't exist)

Thanks.

I have two inputs(number).

How can I duplicate data from first to second and back?

For example, I'll set some value in first input and in second input I get same value and if I set same value in second input I want to get same value in first input.

I think it must be something like that

<div class="first">
    <input type="text" id="email">
</div>  
<div class="second">
    <input type="text" id="name">
</div>
<div id="slider"></div>

<script>
$('#email').change(function(){
    $(this).val($('#name').val());
});
('#name').change(function(){
    $(this).val($('#email').val());
});    
$('#slider').slider();
</script>

https://jsfiddle/Frunky/hzh9zwfo/4/

When I'm trying to put smth in slider() function I get error in console (slider function doesn't exist)

Thanks.

Share Improve this question edited Feb 25, 2017 at 21:11 Frunky asked Feb 25, 2017 at 20:00 FrunkyFrunky 3851 gold badge6 silver badges15 bronze badges 1
  • Show us what you've tried so far – technophobia Commented Feb 25, 2017 at 20:03
Add a ment  | 

2 Answers 2

Reset to default 6

You almost had it...

Try this:

$('#email').keyup(function (){
    $('#name').val($(this).val()); // <-- reverse your selectors here
});
$('#name').keyup(function (){
    $('#email').val($(this).val()); // <-- and here
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first">
  <input type="text" id="email">
</div>  
<div class="second">
  <input type="text" id="name">
</div>

Building on @technophobia 's answer, if you want them to update live, try this:

$('#email').keyup(function(){
    $('#name').val($(this).val());
});
$('#name').keyup(function(){
    $('#email').val($(this).val());
});

本文标签: javascriptMake two input with same data valueStack Overflow