Richa
Richa

Reputation: 4270

one textbox value in another textbox

I want to get value of one textbox and put the same in another textbox. my code is :

<input type="text" value="Keyword" id="one" />
<input type="text" value="Search" id="two" />

<a href="#" id="btn">button</a>

jquery:

var input = $("#one");

    $('#btn').click(function(){
        alert('dgdhjdgj');
        var oneValue = $('#one').val();
        alert("one value "+ oneValue);
        var twoVal =  $('#two').val($(input).attr('value'));
         alert('two Val' + twoVal);
     });

demo is here.

Issue : when I change the value of textbox #one, it does not change the value of #two. thanks in advance.

Upvotes: 0

Views: 3566

Answers (4)

mehmetakifalp
mehmetakifalp

Reputation: 455

write textarea and check it. JSFIDDLE

$("#add").click(function(){
 var thenVal = $("#textarea_first").val();
 $("#textarea_second").val(thenVal);
});

Upvotes: 1

Manish Mishra
Manish Mishra

Reputation: 12375

if all that you want to change the text of second textbox, as soon as you change the text of first textbox, just use jQuery's change event.

just try this then:

$('#one').on("change",function(){
   $('#two').val($(this).val());
});

Upvotes: 0

Quentin
Quentin

Reputation: 943193

$(input).attr('value') gets the value of the value attribute, which is the initial value, not the current value.

You had it right two lines earlier. Use val().

Upvotes: 2

Sridhar R
Sridhar R

Reputation: 20408

Try this

HTML

<input type="text" placeholder="Keyword" id="one" />
<input type="text" placeholder="Search" id="two" />

<a href="#" id="btn">button</a>

Script

$('#btn').click(function() {
   var oneValue = $('#one').val();
   $('#two').val(oneValue)

})

Fiddle

Upvotes: 1

Related Questions