user2001136
user2001136

Reputation:

jQuery - get a value from dynamically created input

I need your help with get a value from created input. I have to get value from input search which is generated by jquery. I have following code but it didn't work:

 $(document).ready(function () {
       $("#raport").click(function() {
        var search = $("#search").val();
        alert (search);
        })    
});

Have you got any ideas?

Upvotes: 1

Views: 6519

Answers (4)

Lokesh Das
Lokesh Das

Reputation: 433

for example if you added a new recipe in your restaurant application page through AJAX, then just use that food and price in this way.

$('#main_food_div').on('change','input:checkbox', function(){
      //Do your jQuery here
        //alert($(this).attr('id'));
         //alert($(this).val());
});

Upvotes: 0

Derek Henderson
Derek Henderson

Reputation: 9706

Assuming it's the #raport element that is generated dynamically, you have to use .on() instead of .click():

$(document).on('click', '#raport', function () {

Upvotes: 1

PSR
PSR

Reputation: 40318

try this for the elements which will come dynamically

$(document).on("click" ,"#raport",function() {

Upvotes: 2

Ted
Ted

Reputation: 4057

You need to create the event in a different way as to work for newly added controls:

$(document).ready(function () {
   $(document).on("click", "#raport" ,function() {
      var search = $("#search").val();
      alert (search);
   });
});

That way the initial listener is added to the document and when an element of id #raport is clicked, even if it is a new control, the code is executed.

Upvotes: 3

Related Questions