0

Is it possible to get the attribute id from JQuery AJAX generated HTML data?

I have these in my index.php

 <script>
 $(document).ready(function () {

 function viewRooms()  
 {  
    $.ajax({  
    url:"rooms.php",
    method:"POST",  
    success:function(data){  
        $('#rooms').html(data); 
    }  
});  
}  
viewRooms();

 $('#save').click(function()
 {
    var room_number = $('#room_number').val();
    var room_type = $('#room_type').val();

        $.ajax({
            url: "save.php",
            method: "POST",
            data: 'room_number='+room_number+'&room_type='+room_type,
                success: function(data)
        });
 }); 
 });
 </script>

 <div id="rooms"></div>

and inside rooms.php

 <input type="text" id="room_number" />
 <input type="text" id="room_type" />
 <button id="save" > </button>

The function save button doesn't work from index.php. also with the id attributed to it.

Please help me if it is possible to do that? Thank's a lot.

2
  • 1
    Put your click listener into your document ready function and after the ajax call succeded Commented Oct 3, 2018 at 6:44
  • Try $('rooms').find('#room_number').val(). Commented Oct 3, 2018 at 6:46

1 Answer 1

1

Wrap save click handler call in a function something like this.

function saveClick() {
$('#save').click(function()
 {
    var room_number = $('#room_number').val();
    var room_type = $('#room_type').val();

        $.ajax({
            url: "save.php",
            method: "POST",
            data: 'room_number='+room_number+'&room_type='+room_type,
                success: function(data)
        });
 });
}

and call this function in ajax success.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.