JavaScript JS How to get value of embeded element after using ajax

Here is my problem. I have external file include.html which i load with $.ajax and append to the body. Content of include.html:

<p id="descriptionText">My description</p>

I want to get value of p#descriptionText after ajax completed, but as the result i see "undefined"

<!DOCTYPE html>
<html lang="en">
<head>    
<meta charset="UTF-8">
    <title>AJAX</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"
            integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
    <script>
        $(document).ready(function () {
            $.ajax({
                url: "include.html",
                dataType: "html",
            }).done(function (response) {
                $('body').html(response);
            });

            console.log($('#descriptionText').val());
        });
    </script>
</head>
<body>

</body>
</html>

Result are the same even if i try to use closure. Example below.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AJAX</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"
            integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
    <script>
        function func() {
            $.ajax({
                url: "include.html",
                dataType: "html",
            }).done(function (response) {
                $('body').html(response);
            });

            return function() {
                console.log($('#descriptionText').val());
            }
        }

        func()();
    </script>
</head>
<body>

</body>
</html>

If i use async: false parameter in $.ajax it works, but i need asynchronous exactly. What is my problem? Thank you!

Answer:1

In a nutshell: Move console.log($('#descriptionText').val()); into the .done() callback of the $.ajax()

like:

.done(function (response) {
    $('body').html(response);
    console.log($('#descriptionText').val());
});

Explanation:

.done() is the success callback when the promise is resolved. A promise in the async land means that the code will be executed at a point later than the current tick of clock. Your console.log is executed at this moment and so will always be undefined since, the promise hasn't been resolved yet.

So, soln is to take care that your console.log is executed after the promise has been resolved. Do this in many possible ways, one simple being like I mentioned earlier: Moving the statement inside the .done() after you have performed the .html() DOM Manipulation.

Answer:2

You messing up with async flow. Move the output into async callback. Like this:

$(document).ready(function () {
    $.ajax({
        url: "include.html",
        dataType: "html",
    }).done(function (response) {
        $('body').html(response);
        console.log($('#descriptionText').val());
    });
});
Answer:3

The issue is indeed related to asynchronous code execution. This line:

        console.log($('#descriptionText').val());

... executes before this executes:

            $('body').html(response);

That is because the callback function you provide to .done is executed asynchronously, when the Ajax response has been received, and JavaScript is reading the event queue to see what next it has to execute. But this will only happen when the currently executing code has completed (i.e. call stack is emptied).

So... to solve this, you have to move your code inside the done callback:

        }).done(function (response) {
            $('body').html(response);
            console.log($('#descriptionText').val());
        });
Answer:4

been using the site for a while - first time posting. I have a problem i am trying to solve. in Javascript (not jQuery or AJAX) i am trying to have 2 drop down menus, and a third field returning an ...

been using the site for a while - first time posting. I have a problem i am trying to solve. in Javascript (not jQuery or AJAX) i am trying to have 2 drop down menus, and a third field returning an ...

  1. javascript drop down menu
  2. javascript drop down select
  3. javascript drop down list multiple selection
  4. javascript drop down list from array
  5. javascript drop down list hide options
  6. javascript drop down value
  7. dropdown in javascript
  8. javascript dropdownlist selected value
  9. javascript drop down box selected value
  10. javascript drop down selected value
  11. javascript set dropdownlist selected value
  12. javascript drop down button
  13. javascript drop down list autocomplete
  14. javascript drop down list events
  15. javascript drop down list get selected value
  16. javascript drop down list default value
  17. javascript drop down search box
  18. javascript drop down list validation
  19. javascript dropdownlist selected text
  20. javascript drop down list search

I have a log like t=2016-08-03T18:47:26+0000 lvl=dbug msg="Event Received" Service=SomeService and I want to turn it into a javascript object like { t: 2016-08-03T18:47:26+0000, lvl: dbug ...

I have a log like t=2016-08-03T18:47:26+0000 lvl=dbug msg="Event Received" Service=SomeService and I want to turn it into a javascript object like { t: 2016-08-03T18:47:26+0000, lvl: dbug ...

  1. javascript regex split by character
  2. javascript regex split string into array
  3. javascript regex split multiple delimiters
  4. javascript regex split on whitespace
  5. javascript regex split string into words
  6. javascript regex split string
  7. javascript regex split csv
  8. javascript regex split camelcase
  9. javascript regex split
  10. javascript regex split url
  11. javascript regex split newline
  12. javascript regex split example
  13. javascript regex split by space
  14. javascript regex split lines
  15. javascript regex split sentences
  16. javascript regex split comma
  17. javascript regex split number
  18. javascript regex split group
  19. javascript regex split not working
  20. javascript regex split every 2 characters

I've got a table with a number of items. I'm trying to show input fields when a user clicks on an edit button, but I don't know how to select only the element within the same row as the edit button. ...

I've got a table with a number of items. I'm trying to show input fields when a user clicks on an edit button, but I don't know how to select only the element within the same row as the edit button. ...

Hey guys i am trying to create empty array in react-native using es6. But while accessing it i get an error data not defined. Here is the code snippet which i am using to initialise the array. ...

Hey guys i am trying to create empty array in react-native using es6. But while accessing it i get an error data not defined. Here is the code snippet which i am using to initialise the array. ...

  1. initialise array react
  2. initialize array react
  3. initialize array react native