MANnDAaR
MANnDAaR

Reputation: 2590

Get value of textbox from another textbox with jquery

I have text box on 1.php like this

<input type="text" id="1" value="somevalue">

&

text box on 2.php like this

<input type="text" id="2" value="">

1.php and 2.php resides on single domain.

I want to get value of textbox1 into textbox2 using jQuery

How can I achieve this ?

Thanks

Upvotes: 0

Views: 1092

Answers (2)

JWhiz
JWhiz

Reputation: 681

You can do something like this.

1) Create a div in 2.php

  <div id="mydiv">
  </div>

2) Load the element onto div

  $('#mydiv').load('1.php #<id-in-1.php>'); 
  <!-- note.. pls use correct id tags.. ids can't start with numbers-->

3) and then access the value by using

    $('#mydiv input').val();

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

If I understand the question correctly, you can do this:

$.get('2.php', function(data) {
  var newVal = $('#2', data).val();
  $('#1').val(newVal);
});

We're forgetting the IDs are numbers, it's just an example here. All we're doing is passing a context option to $(selector, context) to find the ID within the response, and taking its .val() for use.

Upvotes: 1

Related Questions