-2

I have main.aspx and second.aspx.The code in main.aspx:

<form id="form1">
    <div id="main">
    </div>
</form>

And in second.aspx:

<div id="second">
    <asp:Textbox runat="server" id="txt1"></asp:Textbox>
    <input type="button" id="bttn" value="send"></input>
</div>

At the run time I'm loading second.aspx page into main.aspx div id="main".

$("#main").load("second.aspx #second");

I'm trying to get the textbox value from main.aspx.But when I tried

$("#bttn").click(function(){
    var t=$("txt1").val();
    alert(t);
});

It gives nothing.It seems to be never going into the function.How can get the value from the textbox.

Harun Yılmaz
  • 96
  • 1
  • 11

2 Answers2

0

Try this:

$("#bttn").click(function(){
  var t=$("#txt1").val();
  alert(t);
});
MarmiK
  • 5,639
  • 6
  • 40
  • 49
Kaushik Maheta
  • 1,741
  • 1
  • 18
  • 27
0

That is because element do not exist when you are defining the event. You need to either use

1) Event Delegation

$("#main").on('click','#bttn',function(){
  var t = $("#txt1").val();
  alert(t);
});

or

2) Bind the event on success function of load event

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125